# 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
```

---
> My application target version is .Net Framework 4.8

### Step 2: Publish a project
#### :bulb: 發布專案
1. **Publish**

2. Settings -> deploy to **folder**



3. Add **new website**

### 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。)

2. Add **New Login**.
3. Go to **User Mapping** to choose the **database** and the **role**.

4. Check Application Pool Version

### 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**

5. Right-click the certificate you want to grant access to, and then click **All Tasks** > **Manage Private Keys**

6. Click **Advanced** > **Add** > **Select a instance** > Filter the **Location** > Type IIS virtual account name

---
## 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後,在發佈一次)

2. Attach to process (w3wp.exe for .NET Framework projects).
(附加至處理序 (.Net Framework 找 w3wp.exe 處理序)

---
## Republish Files (Demo)
> Folder location -> project solution/Properties/PublishProfiles
> Delete **Properties Folder**
>

# 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:\....
```
## 指定實體路徑到上面輸出的資料夾