asp.net-coreasp.net-core-webapiasp.net-web-api-routing

ASP.NET Core Web API routing is not using https


Question

Why does dotnet run in my ASP.NET Core Web API use http only, and not using https, as well?

Details

I am working on Exercise - Create a minimal API from the ASP.NET team. According to item 3 of Scaffold a Project section of this exercise, the output of dotnet run command should also include https path, say, https://localhost:7200.

But, on my Windows 10 machine, the output includes only http path (as shown below) despite the fact that I have first run the dotnet dev-certs https --trust to trust the self-singing certificate.

Command to trust the certificate

PS C:\Users\MyUserName> dotnet dev-certs https --trust
Trusting the HTTPS development certificate was requested. A confirmation prompt will be displayed if the certificate was not previously trusted. Click yes on the prompt to trust the certificate.
Successfully trusted the existing HTTPS certificate.

Output of dotnet run

PS C:\Users\MyUserName\PizzaStore> dotnet run
Using launch settings from C:\Users\MyUserName\PizzaStore\Properties\launchSettings.json...
Building...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5192
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Users\MyUserName\PizzaStore

Solution

  • We can find launchSettings.json file under the Properties folder.

    Under profiles, we can see that the first default is http, the second is https, and the third is IIS Express. When we execute dotnet run, the default startup item will use http.

    So there are 2 ways to fix the issue.

    1. Use below command to start the application manually.
    dotnet run --launch-profile https
    

    enter image description here

    2. Change the https order in profiles
    {
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:41731",
          "sslPort": 44378
        }
      },
      "profiles": {
        "https": {
          "commandName": "Project",
          "dotnetRunMessages": true,
          "launchBrowser": true,
          "applicationUrl": "https://localhost:7188;http://localhost:5108",
          "environmentVariables": {
             "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "http": {
          "commandName": "Project",
          "dotnetRunMessages": true,
          "launchBrowser": true,
          "applicationUrl": "http://localhost:5108",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
    
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
           }
        }
      }
    }
    

    Then run the command dotnet run, it will use https by default.

    enter image description here