[ACCEPTED]-How can I change Kestrel (AspNetCore) listening port by environment variables in netcore3.1-kestrel

Accepted answer
Score: 16

There are multiple ways to achieve this 14 as documented here.

  1. ASPNETCORE_URLS environment variable.
  2. --urls command-line argument.
  3. urls host configuration key.
  4. UseUrls extension method.

To achieve this using an environment variable, simply 13 create an environment variable called ASPNETCORE_URLS and 12 set the value to the URL you'd like to use

Typically 11 this would be http://+:<port> or https://+:<port>

Another method which, at 10 the time of writing this answer, isn't described 9 above is via the hostsettings.json file.

You 8 can configure the URL & Port by creating 7 a hostsettings.json configuration file and 6 adding the urls key, then add hostsettings.json 5 to your IConfigurationBuilder when building your WebHostBuilder.


Here is the 4 code snippet from the link, in case this 3 link ever goes dead.

Create your hostsettings.json 2 file containing the urls key with your value(s)

    urls: "http://*:5005"

Register 1 hostsettings.json in your IConfigurationBuilder

public class Program
    public static void Main(string[] args)

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: false)
            .AddJsonFile("hostsettings.json", optional: true)

        return WebHost.CreateDefaultBuilder(args)

More Related questions