How to Enable/Disable HTTP/HTTPS in ASP.NET Core 6 – Windows ASP.NET Core Hosting 2024 | Review and Comparison

This is only short tutorial about how to enable or disable or change Kestrel port number in Asp.net core 6 after we run dotnet publish.

There are several ways that we can use, for example:

  1. Change applicationUrl and sqlPort in Properties/launchSettings.json.
  2. If using Visual Studio, change in Project Properties > Debug tab > Web Server Settings.
  3. Comment out app.UseHttpsRedirection(); in Program.cs and etc…

But these seems unable to work if we use dotnet publish and .NET 6. Luckily there is few settings in appsettings.json for us to solve the issue. Example settings are below:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      },
      "Https": {
        "Url": "https://localhost:5001"
      }
    }
  }
}

If we want to disable HTTP for the asp.net code, we just need to remove lines 11 to 13 and the same for HTTPS, if we want to disable HTTPS, just remove lines 14 to 16 and comment out app.UseHttpsRedirection(); in Program.cs.

If we want to change the port number, we just change in the Http and Https there according. By default, the port number will be 5000 for Http and 5001 for Https.

Below is an example result if disable Http and change Https port to 5005:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5005
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /dotnet/razorapp/bin/Release/net6.0/publish/