Visual Studio for ASP.NET Core 中的開發階段 IIS 支援Development-time IIS support in Visual Studio for ASP.NET Core

Sourabh Shirhatti 提供By Sourabh Shirhatti

本文說明針對在 Windows Server 上搭配 IIS 執行的 ASP.NET Core 應用程式所提供的 Visual Studio 偵錯支援。This article describes Visual Studio support for debugging ASP.NET Core apps running with IIS on Windows Server. 本主題會逐步解說如何啟用此案例及設定專案。This topic walks through enabling this scenario and setting up a project.

先決條件Prerequisites

  • 適用于 Windows 的 Visual StudioVisual Studio for Windows
  • ASP.NET 與網頁程式開發 工作負載ASP.NET and web development workload
  • .NET Core 跨平台開發 工作負載.NET Core cross-platform development workload
  • X.509 安全性憑證 (以取得 HTTPS 支援)X.509 security certificate (for HTTPS support)

啟用 IISEnable IIS

  1. 在 Windows 中,瀏覽至 [控制台][程式] > [程式和功能] > [開啟或關閉 Windows 功能] > (畫面左側)。In Windows, navigate to Control Panel > Programs > Programs and Features > Turn Windows features on or off (left side of the screen).
  2. 選取 [Internet Information Services] 核取方塊。Select the Internet Information Services check box. 選取 [確定]。Select OK.

安裝 IIS 可能需要重新啟動系統。The IIS installation may require a system restart.

設定 IISConfigure IIS

IIS 的網站必須含有下列設定:IIS must have a website configured with the following:

  • 主機名稱:通常會使用 預設的網站 與的 主機名稱 localhostHost name: Typically, the Default Web Site is used with a Host name of localhost. 不過,任何具有唯一主機名稱的有效 IIS 網站皆適用。However, any valid IIS website with a unique host name works.
  • 網站繫結Site Binding
    • 針對需要 HTTPS 的應用程式,請搭配憑證針對連接埠 443 建立繫結。For apps that require HTTPS, create a binding to port 443 with a certificate. 通常會使用 [IIS Express 開發憑證],但可使用任何有效的憑證。Typically, the IIS Express Development Certificate is used, but any valid certificate works.
    • 針對使用 HTTP 的應用程式,請確認已存在針對連接埠 80 的繫結,或是為新網站建立針對連接埠 80 的繫結。For apps that use HTTP, confirm the existence of a binding to post 80 or create a binding to port 80 for a new site.
    • 需針對 HTTP 或 HTTPS 使用單一繫結。Use a single binding for either HTTP or HTTPS. 不支援同時針對 HTTP 和 HTTPS 連接埠的繫結。Binding to both HTTP and HTTPS ports simultaneously isn't supported.

在 Visual Studio 中啟用開發階段 IIS 支援Enable development-time IIS support in Visual Studio

  1. 啟動 Visual Studio 安裝程式。Launch the Visual Studio installer.

  2. 針對您打算用於 IIS 開發階段支援的 Visual Studio 安裝,選取 [修改]。Select Modify for the Visual Studio installation that you plan to use for IIS development-time support.

  3. 針對 [ASP.NET 與網頁程式開發] 工作負載,尋找並安裝 [開發時間的 IIS 支援] 元件。For the ASP.NET and web development workload, locate and install the Development time IIS support component.

    該元件會列於工作負載右側 [安裝詳細資料] 中 [開發時間的 IIS 支援] 底下的 [選擇性] 區段中。The component is listed in the Optional section under Development time IIS support in the Installation details panel to the right of the workloads. 此元件會安裝 ASP.NET Core 模組,這是搭配 IIS 執行 ASP.NET Core 應用程式所需的原生 IIS 模組。The component installs the ASP.NET Core Module, which is a native IIS module required to run ASP.NET Core apps with IIS.

設定專案Configure the project

HTTPS 重新導向HTTPS redirection

針對需要 HTTPS 的新專案,請在 [建立新的 ASP.NET Core Web 應用程式] 視窗中,選取 [針對 HTTPS 進行設定] 核取方塊。For a new project that requires HTTPS, select the check box to Configure for HTTPS in the Create a new ASP.NET Core Web Application window. 選取該核取方塊會在建立應用程式時,將 HTTPS 重新導向和 HSTS 中介軟體加入該應用程式。Selecting the check box adds HTTPS Redirection and HSTS Middleware to the app when it's created.

針對需要 HTTPS 的現有專案,請使用 Startup.Configure中的 HTTPS 重新導向和 HSTS 中介軟體。For an existing project that requires HTTPS, use HTTPS Redirection and HSTS Middleware in Startup.Configure. 如需詳細資訊,請參閱 在 ASP.NET Core 中強制使用 HTTPSFor more information, see 在 ASP.NET Core 中強制使用 HTTPS.

針對使用 HTTP 的專案,HTTPS 重新導向和 HSTS 中介軟體並不會被加入至應用程式。For a project that uses HTTP, HTTPS Redirection and HSTS Middleware aren't added to the app. 您不需要進行任何應用程式設定。No app configuration is required.

IIS 啟動設定檔IIS launch profile

建立新的啟動設定檔,以新增開發階段 IIS 支援:Create a new launch profile to add development-time IIS support:

  1. 以滑鼠右鍵按一下 [方案總管] 中的專案。Right-click the project in Solution Explorer. 選取 [屬性] 。Select Properties. 開啟 [ 調試 ] 索引標籤。Open the Debug tab.

  2. 針對 [設定檔],選取 [新增] 按鈕。For Profile, select the New button. 在快顯示窗中,將設定檔命名為 "IIS"。Name the profile "IIS" in the popup window. 選取 [確定] 以建立設定檔。Select OK to create the profile.

  3. 針對 [啟動] 設定,從清單中選取 [IIS]。For the Launch setting, select IIS from the list.

  4. 選取 [啟動瀏覽器] 的核取方塊並提供端點 URL。Select the check box for Launch browser and provide the endpoint URL.

    當應用程式需要 HTTPS 時,請使用 HTTPS 端點 (https://)。When the app requires HTTPS, use an HTTPS endpoint (https://). 針對 HTTP,請使用 HTTP (http://) 端點。For HTTP, use an HTTP (http://) endpoint.

    提供相同的主機名稱和連接埠作為 IIS 設定所指定的早期用途,其通常是 localhostProvide the same host name and port as the IIS configuration specified earlier uses, typically localhost.

    在 URL 結尾提供應用程式的名稱。Provide the name of the app at the end of the URL.

    例如,https://localhost/WebApplication1 (HTTPS) 或 http://localhost/WebApplication1 (HTTP) 皆為有效的端點 URL。For example, https://localhost/WebApplication1 (HTTPS) or http://localhost/WebApplication1 (HTTP) are valid endpoint URLs.

  5. 在 [環境變數] 區段中,選取 [新增] 按鈕。In the Environment variables section, select the Add button. 提供 [名稱] 為 ASPNETCORE_ENVIRONMENT 且 [值] 為 Development 的環境變數。Provide an environment variable with a Name of ASPNETCORE_ENVIRONMENT and a Value of Development.

  6. 在 [Web 伺服器設定] 區域中,將 [應用程式 URL] 設定為用於 [啟動瀏覽器] 端點 URL 的相同值。In the Web Server Settings area, set the App URL to the same value used for the Launch browser endpoint URL.

  7. 針對 Visual Studio 2019 或更新版本中的 [裝載模型],請選取 [預設] 以使用專案所使用的裝載模型。For the Hosting Model setting in Visual Studio 2019 or later, select Default to use the hosting model used by the project. 如果專案在其專案檔中已設定 <AspNetCoreHostingModel> 屬性,則會使用該屬性的值 (InProcessOutOfProcess)。If the project sets the <AspNetCoreHostingModel> property in its project file, the value of the property (InProcess or OutOfProcess) is used. 如果該屬性不存在,便會使用應用程式的預設裝載模型,其為內部處理序。If the property isn't present, the default hosting model of the app is used, which is in-process. 如果應用程式需要與應用程式一般裝載模型不同的明確裝載模型設定,請視需要將 [裝載模型] 設定為 In ProcessOut Of ProcessIf the app requires an explicit hosting model setting different from the app's normal hosting model, set the Hosting Model to either In Process or Out Of Process as needed.

  8. 儲存設定檔。Save the profile.

不使用 Visual Studio 時,請手動將啟動設定檔新增至 Properties 資料夾中的 launchSettings.json (英文) 檔案。When not using Visual Studio, manually add a launch profile to the launchSettings.json file in the Properties folder. 下列範例會將專案設定為使用 HTTPS 通訊協定:The following example configures the profile to use the HTTPS protocol:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "https://localhost/WebApplication1",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS": {
      "commandName": "IIS",
      "launchBrowser": true,
      "launchUrl": "https://localhost/WebApplication1",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

請確認 applicationUrllaunchUrl 端點相符,並使用和 IIS 繫結設定相同的通訊協定 (HTTP 或 HTTPS)。Confirm that the applicationUrl and launchUrl endpoints match and use the same protocol as the IIS binding configuration, either HTTP or HTTPS.

執行專案Run the project

以系統管理員身分執行 Visual Studio:Run Visual Studio as an administrator:

  • 確認建置設定下拉式清單已設定為 [偵錯]。Confirm that the build configuration drop-down list is set to Debug.
  • 將 [ 開始調試 程式] 按鈕設定為 IIS 設定檔,然後選取按鈕以啟動應用程式。Set the Start Debugging button to the IIS profile and select the button to start the app.

如果不是以系統管理員身分執行,Visual Studio 可能會提示您重新啟動。Visual Studio may prompt a restart if not running as an administrator. 若收到提示,請重新啟動 Visual Studio。If prompted, restart Visual Studio.

如果使用未受信任的開發憑證,瀏覽器可能會要求您針對未受信任的憑證建立例外。If an untrusted development certificate is used, the browser may require you to create an exception for the untrusted certificate.

注意

使用 Just My Code 與編譯器最佳化針對發行建置設定進行偵錯會導致體驗變差。Debugging a Release build configuration with Just My Code and compiler optimizations results in a degraded experience. 例如,不會碰到中斷點。For example, break points aren't hit.

其他資源Additional resources

本文說明針對在 Windows Server 上搭配 IIS 執行的 ASP.NET Core 應用程式所提供的 Visual Studio 偵錯支援。This article describes Visual Studio support for debugging ASP.NET Core apps running with IIS on Windows Server. 本主題會逐步解說如何啟用此案例及設定專案。This topic walks through enabling this scenario and setting up a project.

先決條件Prerequisites

  • 適用于 Windows 的 Visual StudioVisual Studio for Windows
  • ASP.NET 與網頁程式開發 工作負載ASP.NET and web development workload
  • .NET Core 跨平台開發 工作負載.NET Core cross-platform development workload
  • X.509 安全性憑證 (以取得 HTTPS 支援)X.509 security certificate (for HTTPS support)

啟用 IISEnable IIS

  1. 在 Windows 中,瀏覽至 [控制台][程式] > [程式和功能] > [開啟或關閉 Windows 功能] > (畫面左側)。In Windows, navigate to Control Panel > Programs > Programs and Features > Turn Windows features on or off (left side of the screen).
  2. 選取 [Internet Information Services] 核取方塊。Select the Internet Information Services check box. 選取 [確定]。Select OK.

安裝 IIS 可能需要重新啟動系統。The IIS installation may require a system restart.

設定 IISConfigure IIS

IIS 的網站必須含有下列設定:IIS must have a website configured with the following:

  • 主機名稱:通常會使用 預設的網站 與的 主機名稱 localhostHost name: Typically, the Default Web Site is used with a Host name of localhost. 不過,任何具有唯一主機名稱的有效 IIS 網站皆適用。However, any valid IIS website with a unique host name works.
  • 網站繫結Site Binding
    • 針對需要 HTTPS 的應用程式,請搭配憑證針對連接埠 443 建立繫結。For apps that require HTTPS, create a binding to port 443 with a certificate. 通常會使用 [IIS Express 開發憑證],但可使用任何有效的憑證。Typically, the IIS Express Development Certificate is used, but any valid certificate works.
    • 針對使用 HTTP 的應用程式,請確認已存在針對連接埠 80 的繫結,或是為新網站建立針對連接埠 80 的繫結。For apps that use HTTP, confirm the existence of a binding to post 80 or create a binding to port 80 for a new site.
    • 需針對 HTTP 或 HTTPS 使用單一繫結。Use a single binding for either HTTP or HTTPS. 不支援同時針對 HTTP 和 HTTPS 連接埠的繫結。Binding to both HTTP and HTTPS ports simultaneously isn't supported.

在 Visual Studio 中啟用開發階段 IIS 支援Enable development-time IIS support in Visual Studio

  1. 啟動 Visual Studio 安裝程式。Launch the Visual Studio installer.

  2. 針對您打算用於 IIS 開發階段支援的 Visual Studio 安裝,選取 [修改]。Select Modify for the Visual Studio installation that you plan to use for IIS development-time support.

  3. 針對 [ASP.NET 與網頁程式開發] 工作負載,尋找並安裝 [開發時間的 IIS 支援] 元件。For the ASP.NET and web development workload, locate and install the Development time IIS support component.

    該元件會列於工作負載右側 [安裝詳細資料] 中 [開發時間的 IIS 支援] 底下的 [選擇性] 區段中。The component is listed in the Optional section under Development time IIS support in the Installation details panel to the right of the workloads. 此元件會安裝 ASP.NET Core 模組,這是搭配 IIS 執行 ASP.NET Core 應用程式所需的原生 IIS 模組。The component installs the ASP.NET Core Module, which is a native IIS module required to run ASP.NET Core apps with IIS.

設定專案Configure the project

HTTPS 重新導向HTTPS redirection

針對需要 HTTPS 的新專案,請在 [建立新的 ASP.NET Core Web 應用程式] 視窗中,選取 [針對 HTTPS 進行設定] 核取方塊。For a new project that requires HTTPS, select the check box to Configure for HTTPS in the Create a new ASP.NET Core Web Application window. 選取該核取方塊會在建立應用程式時,將 HTTPS 重新導向和 HSTS 中介軟體加入該應用程式。Selecting the check box adds HTTPS Redirection and HSTS Middleware to the app when it's created.

針對需要 HTTPS 的現有專案,請使用 Startup.Configure中的 HTTPS 重新導向和 HSTS 中介軟體。For an existing project that requires HTTPS, use HTTPS Redirection and HSTS Middleware in Startup.Configure. 如需詳細資訊,請參閱 在 ASP.NET Core 中強制使用 HTTPSFor more information, see 在 ASP.NET Core 中強制使用 HTTPS.

針對使用 HTTP 的專案,HTTPS 重新導向和 HSTS 中介軟體並不會被加入至應用程式。For a project that uses HTTP, HTTPS Redirection and HSTS Middleware aren't added to the app. 您不需要進行任何應用程式設定。No app configuration is required.

IIS 啟動設定檔IIS launch profile

建立新的啟動設定檔,以新增開發階段 IIS 支援:Create a new launch profile to add development-time IIS support:

  1. 以滑鼠右鍵按一下 [方案總管] 中的專案。Right-click the project in Solution Explorer. 選取 [屬性] 。Select Properties. 開啟 [ 調試 ] 索引標籤。Open the Debug tab.

  2. 針對 [設定檔],選取 [新增] 按鈕。For Profile, select the New button. 在快顯示窗中,將設定檔命名為 "IIS"。Name the profile "IIS" in the popup window. 選取 [確定] 以建立設定檔。Select OK to create the profile.

  3. 針對 [啟動] 設定,從清單中選取 [IIS]。For the Launch setting, select IIS from the list.

  4. 選取 [啟動瀏覽器] 的核取方塊並提供端點 URL。Select the check box for Launch browser and provide the endpoint URL.

    當應用程式需要 HTTPS 時,請使用 HTTPS 端點 (https://)。When the app requires HTTPS, use an HTTPS endpoint (https://). 針對 HTTP,請使用 HTTP (http://) 端點。For HTTP, use an HTTP (http://) endpoint.

    提供相同的主機名稱和連接埠作為 IIS 設定所指定的早期用途,其通常是 localhostProvide the same host name and port as the IIS configuration specified earlier uses, typically localhost.

    在 URL 結尾提供應用程式的名稱。Provide the name of the app at the end of the URL.

    例如,https://localhost/WebApplication1 (HTTPS) 或 http://localhost/WebApplication1 (HTTP) 皆為有效的端點 URL。For example, https://localhost/WebApplication1 (HTTPS) or http://localhost/WebApplication1 (HTTP) are valid endpoint URLs.

  5. 在 [環境變數] 區段中,選取 [新增] 按鈕。In the Environment variables section, select the Add button. 提供 [名稱] 為 ASPNETCORE_ENVIRONMENT 且 [值] 為 Development 的環境變數。Provide an environment variable with a Name of ASPNETCORE_ENVIRONMENT and a Value of Development.

  6. 在 [Web 伺服器設定] 區域中,將 [應用程式 URL] 設定為用於 [啟動瀏覽器] 端點 URL 的相同值。In the Web Server Settings area, set the App URL to the same value used for the Launch browser endpoint URL.

  7. 針對 Visual Studio 2019 或更新版本中的 [裝載模型],請選取 [預設] 以使用專案所使用的裝載模型。For the Hosting Model setting in Visual Studio 2019 or later, select Default to use the hosting model used by the project. 如果專案在其專案檔中已設定 <AspNetCoreHostingModel> 屬性,則會使用該屬性的值 (InProcessOutOfProcess)。If the project sets the <AspNetCoreHostingModel> property in its project file, the value of the property (InProcess or OutOfProcess) is used. 如果該屬性不存在,便會使用應用程式的預設裝載模型,其為外部處理序。If the property isn't present, the default hosting model of the app is used, which is out-of-process. 如果應用程式需要與應用程式一般裝載模型不同的明確裝載模型設定,請視需要將 [裝載模型] 設定為 In ProcessOut Of ProcessIf the app requires an explicit hosting model setting different from the app's normal hosting model, set the Hosting Model to either In Process or Out Of Process as needed.

  8. 儲存設定檔。Save the profile.

不使用 Visual Studio 時,請手動將啟動設定檔新增至 Properties 資料夾中的 launchSettings.json (英文) 檔案。When not using Visual Studio, manually add a launch profile to the launchSettings.json file in the Properties folder. 下列範例會將專案設定為使用 HTTPS 通訊協定:The following example configures the profile to use the HTTPS protocol:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "https://localhost/WebApplication1",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS": {
      "commandName": "IIS",
      "launchBrowser": true,
      "launchUrl": "https://localhost/WebApplication1",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

請確認 applicationUrllaunchUrl 端點相符,並使用和 IIS 繫結設定相同的通訊協定 (HTTP 或 HTTPS)。Confirm that the applicationUrl and launchUrl endpoints match and use the same protocol as the IIS binding configuration, either HTTP or HTTPS.

執行專案Run the project

以系統管理員身分執行 Visual Studio:Run Visual Studio as an administrator:

  • 確認建置設定下拉式清單已設定為 [偵錯]。Confirm that the build configuration drop-down list is set to Debug.
  • 將 [ 開始調試 程式] 按鈕設定為 IIS 設定檔,然後選取按鈕以啟動應用程式。Set the Start Debugging button to the IIS profile and select the button to start the app.

如果不是以系統管理員身分執行,Visual Studio 可能會提示您重新啟動。Visual Studio may prompt a restart if not running as an administrator. 若收到提示,請重新啟動 Visual Studio。If prompted, restart Visual Studio.

如果使用未受信任的開發憑證,瀏覽器可能會要求您針對未受信任的憑證建立例外。If an untrusted development certificate is used, the browser may require you to create an exception for the untrusted certificate.

注意

使用 Just My Code 與編譯器最佳化針對發行建置設定進行偵錯會導致體驗變差。Debugging a Release build configuration with Just My Code and compiler optimizations results in a degraded experience. 例如,不會碰到中斷點。For example, break points aren't hit.

其他資源Additional resources