將 ASP.NET Core 應用程式發佈到 IISPublish an ASP.NET Core app to IIS

本教學課程會示範如何在 IIS 伺服器上裝載 ASP.NET Core 應用程式。This tutorial shows how to host an ASP.NET Core app on an IIS server.

本教學課程涵蓋下列主題:This tutorial covers the following subjects:

  • 在 Windows Server 上安裝 .NET Core 裝載套件組合。Install the .NET Core Hosting Bundle on Windows Server.
  • 在 IIS 管理員中建立 IIS 網站。Create an IIS site in IIS Manager.
  • 部署 ASP.NET Core 應用程式。Deploy an ASP.NET Core app.

必要條件Prerequisites

  • 在部署電腦上安裝 .NET Core SDK.NET Core SDK installed on the development machine.
  • 使用 Web Server (IIS) 伺服器角色設定的 Windows Server。Windows Server configured with the Web Server (IIS) server role. 若您的伺服器並未設為使用 IIS 來裝載網站,請遵循 在使用 IIS 的 Windows 上裝載 ASP.NET Core 一文中<IIS 組態> 一節內的指導,然後返回本教學課程。If your server isn't configured to host websites with IIS, follow the guidance in the IIS configuration section of the 在使用 IIS 的 Windows 上裝載 ASP.NET Core article and then return to this tutorial.

警告

IIS 組態和網站安全性涉及本教學課程並未涵蓋的概念。IIS configuration and website security involve concepts that aren't covered by this tutorial. 請諮詢 Microsoft IIS 文件使用 IIS 進行裝載的 ASP.NET Core 文章,再於 IIS 上裝載生產應用程式。Consult the IIS guidance in the Microsoft IIS documentation and the ASP.NET Core article on hosting with IIS before hosting production apps on IIS.

本教學課程中尚未涵蓋的 IIS 裝載重要案例包括:Important scenarios for IIS hosting not covered by this tutorial include:

安裝 .NET Core 裝載套件組合Install the .NET Core Hosting Bundle

在 IIS 伺服器上安裝 .NET Core 裝載套件組合Install the .NET Core Hosting Bundle on the IIS server. 套件組合會安裝 .NET Core 執行時間、.NET Core 程式庫和 ASP.NET Core 模組The bundle installs the .NET Core Runtime, .NET Core Library, and the ASP.NET Core Module. 此模組可讓 ASP.NET Core 應用程式在 IIS 背後執行。The module allows ASP.NET Core apps to run behind IIS.

使用下列連結下載安裝程式:Download the installer using the following link:

目前的 .NET Core 裝載套件組合安裝程式 (直接下載)Current .NET Core Hosting Bundle installer (direct download)

  1. 在 IIS 伺服器上執行安裝程式。Run the installer on the IIS server.

  2. 重新開機伺服器,或 net stop was /y 在命令列介面中執行,然後執行 net start w3svcRestart the server or execute net stop was /y followed by net start w3svc in a command shell.

建立 IIS 網站Create the IIS site

  1. 在 IIS 伺服器上,建立資料夾以包含應用程式的發佈資料夾和檔案。On the IIS server, create a folder to contain the app's published folders and files. 在下列步驟中,您提供資料夾路徑給 IIS,作為應用程式的實體路徑。In a following step, the folder's path is provided to IIS as the physical path to the app. 如需應用程式之部署資料夾和檔案配置的詳細資訊,請參閱 ASP.NET Core 目錄結構For more information on an app's deployment folder and file layout, see ASP.NET Core 目錄結構.

  2. 在 [IIS 管理員] 中 ,在 [連線] 面板中 開啟伺服器的節點。In IIS Manager, open the server's node in the Connections panel. 以滑鼠右鍵按一下 [網站] 資料夾。Right-click the Sites folder. 從操作功能表選取 [新增網站] 。Select Add Website from the contextual menu.

  3. 提供 網站名稱 ,並將 實體路徑 設定為您建立的應用程式部署資料夾。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 .

    警告

    使用最上層萬用字元繫結 (http://*:80/http://+:80)。Top-level wildcard bindings (http://*:80/ and http://+:80) should not be used. 最上層萬用字元繫結可能暴露您的應用程式安全性弱點。Top-level wildcard bindings can open up your app to security vulnerabilities. 這對強式與弱式萬用字元皆適用。This applies to both strong and weak wildcards. 請使用明確主機名稱,而非萬用字元。Use explicit host names rather than wildcards. 若您擁有整個父網域 (與具弱點的 *.com 相對) 的控制權,則子網域萬用字元繫結 (例如 *.mysub.com) 就沒有此安全性風險。Subdomain wildcard binding (for example, *.mysub.com) doesn't have this security risk if you control the entire parent domain (as opposed to *.com, which is vulnerable). 如需詳細資訊,請參閱 rfc7230 5.4 節See rfc7230 section-5.4 for more information.

  4. 確認處理序模型身分識別具有適當的權限。Confirm the process model identity has the proper permissions.

    如果應用程式集區的預設身分識別 ( 進程模型 > Identity ) 已從變更 ApplicationPoolIdentity 為其他身分識別,請確認新的身分識別具有存取應用程式資料夾、資料庫和其他必要資源的必要許可權。If the default identity of the app pool ( Process Model > Identity ) is changed from ApplicationPoolIdentity to another identity, verify that the new identity has the required permissions to access the app's folder, database, and other required resources. 例如,應用程式集區需要針對應用程式讀取和寫入檔案的資料夾取得讀取和寫入權限。For example, the app pool requires read and write access to folders where the app reads and writes files.

建立 ASP.NET Core Razor 頁面應用程式Create an ASP.NET Core Razor Pages app

遵循 ASP.NET Core 使用者入門 教學課程來建立 Razor 頁面應用程式。Follow the ASP.NET Core 使用者入門 tutorial to create a Razor Pages app.

發佈及部署應用程式Publish and deploy the app

「發佈應用程式」 表示產生可由伺服器裝載的編譯應用程式。Publish an app means to produce a compiled app that can be hosted by a server. 「部署應用程式」 表示將發佈後的應用程式移動到裝載系統。Deploy an app means to move the published app to a hosting system. 發佈步驟會由 .NET Core SDK 處理,部署步驟則是可以透過各種方式處理。The publish step is handled by the .NET Core SDK, while the deployment step can be handled by a variety of approaches. 本教學課程採用「資料夾」 部署方法,其中:This tutorial adopts the folder deployment approach, where:

  • 應用程式會發佈到資料夾。The app is published to a folder.
  • 資料夾內容會移動到 IIS 網站的資料夾 (指向 IIS 管理員中網站的 實體路徑 )。The folder's contents are moved to the IIS site's folder (the Physical path to the site in IIS Manager).
  1. 以滑鼠右鍵按一下 方案總管 中的專案,然後選取 [ 發行 ]。Right-click on the project in Solution Explorer and select Publish .
  2. 在 [挑選發佈目標] 對話方塊中,選取 [資料夾] 發佈選項。In the Pick a publish target dialog, select the Folder publish option.
  3. 設定 [資料夾或檔案共用] 路徑。Set the Folder or File Share path.
    • 若您針對部署電腦上提供為網路共用的 IIS 網站建立資料夾,則請提供指向共用的路徑。If you created a folder for the IIS site that's available on the development machine as a network share, provide the path to the share. 目前的使用者必須具備寫入存取權限才能發佈到共用。The current user must have write access to publish to the share.
    • 如果您無法直接部署至 IIS 伺服器上的 IIS 網站資料夾,請發行至卸載式媒體上的資料夾,並實際將已發佈的應用程式移至伺服器上的 IIS 網站資料夾,也就是 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. 將資料夾的內容移 bin/Release/{TARGET FRAMEWORK}/publish 至伺服器上的 iis 網站資料夾,也就是 [Iis 管理員] 中的網站 實體路徑Move the contents of the bin/Release/{TARGET FRAMEWORK}/publish folder to the IIS site folder on the server, which is the site's Physical path in IIS Manager.

瀏覽網站Browse the website

應用程式會在收到第一個要求時,在瀏覽器中提供存取。The app is accessible in a browser after it receives the first request. 在您於 IIS 管理員中為網站建立的端點繫結處,對應用程式發出要求。Make a request to the app at the endpoint binding that you established in IIS Manager for the site.

後續步驟Next steps

在本教學課程中,您已了解如何:In this tutorial, you learned how to:

  • 在 Windows Server 上安裝 .NET Core 裝載套件組合。Install the .NET Core Hosting Bundle on Windows Server.
  • 在 IIS 管理員中建立 IIS 網站。Create an IIS site in IIS Manager.
  • 部署 ASP.NET Core 應用程式。Deploy an ASP.NET Core app.

若要深入了解在 IIS 上裝載 ASP.NET Core 應用程式,請參閱 IIS 概觀文章:To learn more about hosting ASP.NET Core apps on IIS, see the IIS Overview article:

其他資源Additional resources

ASP.NET Core 文件集中的文章Articles in the ASP.NET Core documentation set

與 ASP.NET Core 應用程式部署相關的文章Articles pertaining to ASP.NET Core app deployment

IIS HTTPS 組態的相關文章Articles on IIS HTTPS configuration

IIS 和 Windows Server 的相關文章Articles on IIS and Windows Server

IIS 系統管理員的部署資源Deployment resources for IIS administrators