asp.net-core

Publishing and Deployment

Kestrel. Configuring Listening Address

Using Kestrel you can specify port using next approaches:

  1. Defining ASPNETCORE_URLS environment variable.

    Windows

    SET ASPNETCORE_URLS=https://0.0.0.0:5001

    OS X

    export ASPNETCORE_URLS=https://0.0.0.0:5001
  2. Via command line passing --server.urls parameter

    dotnet run --server.urls=https://0.0.0.0:5001
  3. Using UseUrls() method

    var builder = new WebHostBuilder()
                  .UseKestrel()
                  .UseUrls("https://0.0.0.0:5001")
  4. Defining server.urls setting in configuration source.

Next sample use hosting.json file for example.

Add `hosting.json` with the following content to you project:

    {
       "server.urls": "https://<ip address>:<port>" 
    }

Examples of posible values:

  • listen 5000 on any IP4 and IP6 addresses from any interface:

     "server.urls": "https://*:5000" 

    or

     "server.urls": "https://::5000;https://0.0.0.0:5000"
  • listen 5000 on every IP4 address:

     "server.urls": "https://0.0.0.0:5000"

One should be carefully and not use https://*:5000;https://::5000, https://::5000;https://*:5000, https://*:5000;https://0.0.0.0:5000 or https://*:5000;https://0.0.0.0:5000 because it will require to register IP6 address :: or IP4 address 0.0.0.0 twice

Add file to publishOptions in project.json

"publishOptions": {
"include": [
    "hosting.json",
    ...
  ]
}

and in entry point for the application call .UseConfiguration(config) when creating WebHostBuilder:

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("hosting.json", optional: true)
        .Build();

    var host = new WebHostBuilder()
        .UseConfiguration(config)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow