本教學課程會示範如何在 IIS 伺服器上裝載 ASP.NET Core 應用程式。
本教學課程涵蓋下列主題:
- 在 Windows Server 上安裝 .NET Core 裝載套件組合。
- 在 IIS 管理員中建立 IIS 網站。
- 部署 ASP.NET Core 應用程式。
Prerequisites
- 在部署電腦上安裝 .NET Core SDK。
- 使用 Web Server (IIS) 伺服器角色設定的 Windows Server。 If your server isn't configured to host websites with IIS, follow the guidance in the IIS configuration section of the Host ASP.NET Core on Windows with IIS article and then return to this tutorial.
Warning
IIS 組態和網站安全性涉及本教學課程並未涵蓋的概念。 請諮詢 Microsoft IIS 文件和使用 IIS 進行裝載的 ASP.NET Core 文章,再於 IIS 上裝載生產應用程式。
本教學課程中尚未涵蓋的 IIS 裝載重要案例包括:
- 建立 ASP.NET Core 資料保護的登錄區
- 設定應用程式集區的存取控制清單 (ACL)
- 為了聚焦在 IIS 部署概念,本教學課程會在並未於 IIS 上設定 HTTPS 安全性的情況下部署應用程式。 For more information on hosting an app enabled for HTTPS protocol, see the security topics in the Additional resources section of this article. 使用 IIS 在 Windows 上裝載 ASP.NET Core 一文中提供裝載 ASP.NET Core 應用程式的進一步指導。
安裝 .NET Core 裝載套件組合
在 IIS 伺服器上安裝 .NET Core 裝載套件組合。 套件組合會安裝 .NET Core 執行階段、.NET Core 程式庫和 ASP.NET Core 模組。 此模組可讓 ASP.NET Core 應用程式在 IIS 背後執行。
使用下列連結下載安裝程式:
目前的 .NET Core 裝載套件組合安裝程式 (直接下載)
在 IIS 伺服器上執行安裝程式。
重新啟動伺服器,或在命令殼層中執行
net stop was /y,接著執行net start w3svc。
建立 IIS 網站
在 IIS 伺服器上,建立資料夾以包含應用程式的發佈資料夾和檔案。 在下列步驟中,您提供資料夾路徑給 IIS,作為應用程式的實體路徑。 如需應用程式的部署資料夾和檔案配置的相關詳細資訊,請參閱 ASP.NET Core 目錄結構。
In IIS Manager, open the server's node in the Connections panel. Right-click the Sites folder. Select Add Website from the contextual menu.
Provide a Site name and set the Physical path to the app's deployment folder that you created. Provide the Binding configuration and create the website by selecting OK.
Warning
Top-level wildcard bindings (
http://*:80/andhttp://+:80) should not be used. 最上層萬用字元繫結可能暴露您的應用程式安全性弱點。 這對強式與弱式萬用字元皆適用。 請使用明確主機名稱,而非萬用字元。 若您擁有整個父網域 (與具弱點的*.mysub.com相對) 的控制權,則子網域萬用字元繫結 (例如*.com) 就沒有此安全性風險。 如需詳細資訊,請參閱 RFC 9110:HTTP 語意 (第 7.2. 節主機和 :authority)。確認進程模型識別具有適當的許可權。
If the default identity of the app pool (Process Model>Identity) is changed from
ApplicationPoolIdentityto another identity, verify that the new identity has the required permissions to access the app's folder, database, and other required resources. 例如,應用程式集區需要針對應用程式讀取和寫入檔案的資料夾取得讀取和寫入權限。
建立 ASP.NET Core 應用程式
建立任何類型的 ASP.NET Core 伺服器型應用程式。
Note
本教學課程是以使用 IIS 裝載伺服器端 ASP.NET Core 應用程式為基礎,包括 Blazor Web App。 如需使用 IIS 裝載和部署獨立Blazor WebAssembly應用程式的指引,請參閱使用 IIS 裝載和部署 ASP.NET CoreBlazor WebAssembly。
發佈及部署應用程式
「發佈應用程式」表示產生可由伺服器裝載的編譯應用程式。 「部署應用程式」表示將發佈後的應用程式移動到裝載系統。 發佈步驟會由 .NET Core SDK 處理,部署步驟則是可以透過各種方式處理。 This tutorial adopts the folder deployment approach, where:
- 應用程式會發佈到資料夾。
- The folder's contents are moved to the IIS site's folder (the Physical path to the site in IIS Manager).
- Right-click on the project in Solution Explorer and select Publish.
- 在 [挑選發佈目標] 對話方塊中,選取 [資料夾] 發佈選項。
- 設定 [資料夾或檔案共用] 路徑。
- 若您針對部署電腦上提供為網路共用的 IIS 網站建立資料夾,則請提供指向共用的路徑。 目前的使用者必須具備寫入存取權限才能發佈到共用。
- If you're unable to deploy directly to the IIS site folder on the IIS server, publish to a folder on removable media and physically move the published app to the IIS site folder on the server, which is the site's Physical path in IIS Manager. Move the contents of the
bin/Release/{TARGET FRAMEWORK}/publishfolder to the IIS site folder on the server, which is the site's Physical path in IIS Manager.
- Select the Publish button.
瀏覽網站
應用程式會在收到第一個要求時,在瀏覽器中提供存取。 在您於 IIS 管理員中為網站建立的端點繫結處,對應用程式發出要求。
Next steps
在本教學課程中,您已了解如何:
- 在 Windows Server 上安裝 .NET Core 裝載套件組合。
- 在 IIS 管理員中建立 IIS 網站。
- 部署 ASP.NET Core 應用程式。
若要深入了解在 IIS 上裝載 ASP.NET Core 應用程式,請參閱 IIS 概觀文章:
Additional resources
ASP.NET Core 文件集中的文章
- 適用於 IIS 的 ASP.NET Core 模組 (ANCM)
- ASP.NET Core 目錄結構
- 針對 Azure App Service 和 IIS 上的 ASP.NET Core 進行疑難排解
- 在 ASP.NET Core 中強制執行 HTTPS
- IIS 上的 WebSocket
與 ASP.NET Core 應用程式部署相關的文章
- 使用 Visual Studio 將 ASP.NET Core 應用程式發行到 Azure
- 使用 Visual Studio Code,將 ASP.NET Core 應用程式發佈至 Azure
- 適用於 ASP.NET Core 應用程式部署的 Visual Studio 發行設定檔 (.pubxml)