---
# System prepended metadata

title: How to Publish .NET Framework Applications to IIS
tags: [.Net Framework]

---

# How to Publish .NET Framework Applications to IIS

> Setup IIS in localhost as web development server :arrow_right: 

### Step 1: Check Windows optional feature
#### :bulb: 開啟或關閉Windows功能
```
Press win + R  and type optionalfeatures
```
![image](https://hackmd.io/_uploads/BysSyoEwJx.png)
---
> My application target version is .Net Framework 4.8

![image](https://hackmd.io/_uploads/Bkx0gs4wJg.png)
### Step 2: Publish a project
#### :bulb: 發布專案
1. **Publish**
    ![image](https://hackmd.io/_uploads/SJvzfs4D1e.png)
2. Settings -> deploy to **folder**
![image](https://hackmd.io/_uploads/rJ88pjNPJx.png)
![image](https://hackmd.io/_uploads/rJaY6jEDkx.png)
![image](https://hackmd.io/_uploads/S1C-AiVvkg.png)
3. Add **new website**
![image](https://hackmd.io/_uploads/Sko7yhVwye.png)

### Step 3: Check SQL Server IIS AppPool Permission
#### :bulb: 確認資料庫存取權限
:bulb: **Hint:** If you encounter the error "user login failed", it means the IIS application pool for your project does not have permission to access the database.
(如果遇到 "使用者登入失敗" 的錯誤，這表示專案的 IIS 應用程式集區沒有權限存取資料庫。)
1. Check the **identity** of the IIS Application Pool.
:bulb: **Hint:** The identity is usually set to **ApplicationPoolIdentity**. The default name format is **IIS AppPool\ApplicationName**
（識別通常設定為 ApplicationPoolIdentity，預設名稱格式為 IIS AppPool\ApplicationName。）
![image](https://hackmd.io/_uploads/By1xwaNP1l.png)
2. Add **New Login**.
3. Go to **User Mapping** to choose the **database** and the **role**.
![image](https://hackmd.io/_uploads/SJVzG34vyg.png)
4. Check Application Pool Version
![image](https://hackmd.io/_uploads/ByEewJSDJx.png)


### Step 4: Check certificate permission (always encrypt) (optional)
#### :bulb: 確認SQL憑證權限
:::info
IIS **cannot recognize local user certificates** by default, because it uses the **computer account** to manage certificates.***(IIS 使用的是本機電腦)*** To resolve this, you need to **manually add the certificate** to the **computer account's certificate store** and ensure the **IIS AppPool identity** has been granted **access to the private key**.
:::
1. Press **win + R** and Type **mmc** (Microsoft Management Console)
2. Click **File** > **Add/Remove Snap-in** > choose **Certificates**
3. Click **Computer account** then Finish
4. Click the Certificates node and then **Personal** > **Certificates**
![image](https://hackmd.io/_uploads/HkLFykHv1e.png)
5. Right-click the certificate you want to grant access to, and then click **All Tasks** > **Manage Private Keys**
![image](https://hackmd.io/_uploads/SkKklJBDkx.png)
6. Click **Advanced** > **Add** > **Select a instance** > Filter the **Location** > Type IIS virtual account name
![image](https://hackmd.io/_uploads/ByhmlJHPyg.png)
---
## Debugging a Project Deployed to IIS
### Tools and Steps to Set Breakpoints in IIS-hosted Applications
#### :bulb: 對部署在IIS專案進行偵錯
1. Change the configuration to Debug, then publish again.
(組態要改成Debug後，在發佈一次)
![image](https://hackmd.io/_uploads/r1zEQbBDyg.png)
2. Attach to process (w3wp.exe for .NET Framework projects).
(附加至處理序 (.Net Framework 找 w3wp.exe 處理序)
![image](https://hackmd.io/_uploads/S1IzJ-HD1l.png)
---
## Republish Files (Demo)
> Folder location -> project solution/Properties/PublishProfiles
> Delete **Properties Folder**
> 
![image](https://hackmd.io/_uploads/rJoPIsNv1e.png)

# Create Sub Application(.Net Core) On Same port

## 主網站右鍵新增應用程式
## 別名 (會加在Route後面)
- 原本的主網站Route = http://localhost:3000
- 子網站Route = http://localhost:3000/別名
## .Net Core要安裝Hosting Bundle
## 將專案publish
```shell
dotnet publish -c Release -o C:\....
```
## 指定實體路徑到上面輸出的資料夾