Development-time IIS support in Visual Studio for ASP.NET Core



By Sourabh Shirhatti and Luke Latham

This article describes Visual Studio support for debugging ASP.NET Core apps running behind IIS on Windows Server. This topic walks through enabling this feature and setting up a project.

Prerequisites

Visual Studio for Windows

  • ASP.NET and web development workload
  • .NET Core cross-platform development workload
  • X.509 security certificate

Enable IIS

  1. Navigate to Control Panel > Programs > Programs and Features > Turn Windows features on or off (left side of the screen).
  2. Select the Internet Information Services check box.

Windows Features showing Internet Information Services check box checked as a black square (not a checkmark) indicating that some of the IIS features are enabled

The IIS installation may require a system restart.

Configure IIS

IIS must have a website configured with the following:

  • A host name that matches the app’s launch profile URL host name.
  • Binding for port 443 with an assigned certificate.

For example, the Host name for an added website is set to “localhost” (the launch profile will also use “localhost” later in this topic). The port is set to “443” (HTTPS). The IIS Express Development Certificate is assigned to the website, but any valid certificate works:

Add Website window in IIS showing the binding set for localhost on port 443 with a certificate assigned.

If the IIS installation already has a Default Web Site with a host name that matches the app’s launch profile URL host name:

  • Add a port binding for port 443 (HTTPS).
  • Assign a valid certificate to the website.

Enable development-time IIS support in Visual Studio

  1. Launch the Visual Studio installer.
  2. Select the Development time IIS support component. The component is listed as optional in the Summary panel for the ASP.NET and web development workload. The component installs the ASP.NET Core Module, which is a native IIS module required to run ASP.NET Core apps behind IIS in a reverse proxy configuration.

Modifying Visual Studio features: The Workloads tab is selected. In the Web and Cloud section, the ASP.NET and web development panel is selected. On the right in the Optional area of the Summary panel, there's a check box for Development time IIS support.

Configure the project

HTTPS redirection

For a new project, select the check box to Configure for HTTPS in the New ASP.NET Core Web Application window:

New ASP.NET Core Web Application window with the Configure for HTTPS check box selected.

In an existing project, use HTTPS Redirection Middleware in Startup.Configure by calling the UseHttpsRedirection extension method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)

    if (env.IsDevelopment())
    
        app.UseDeveloperExceptionPage();
    
    else
    
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();

    app.UseMvc();

IIS launch profile

Create a new launch profile to add development-time IIS support:

  1. For Profile, select the New button. Name the profile “IIS” in the popup window. Select OK to create the profile.
  2. For the Launch setting, select IIS from the list.
  3. Select the check box for Launch browser and provide the endpoint URL. Use the HTTPS protocol. This example uses https://localhost/WebApplication1.
  4. In the Environment variables section, select the Add button. Provide an environment variable with a key of ASPNETCORE_ENVIRONMENT and a value of Development.
  5. In the Web Server Settings area, set the App URL. This example uses https://localhost/WebApplication1.
  6. Save the profile.

Project properties window with the Debug tab selected. The Profile and Launch settings are set to IIS. The Launch browser feature is enabled with an address of https://localhost/WebApplication1. The same address is also provided in the App URL field of the Web Server Settings area.

Alternatively, manually add a launch profile to the launchSettings.json file in the app:


  "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"
      
    
  

Run the project

In the VS UI, set the Run button to the IIS profile and select the button to start the app:

Run button in the VS toolbar set to the "IIS" profile.

Visual Studio may prompt a restart if not running as an administrator. 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.

Additional resources



source_link
https://www.asp.net