Publishing and Deployment
Kestrel. Configuring Listening Address
Using Kestrel you can specify port using next approaches:
-
Defining
ASPNETCORE_URLSenvironment variable.Windows
SET ASPNETCORE_URLS=https://0.0.0.0:5001OS X
export ASPNETCORE_URLS=https://0.0.0.0:5001 -
Via command line passing
--server.urlsparameterdotnet run --server.urls=https://0.0.0.0:5001 -
Using
UseUrls()methodvar builder = new WebHostBuilder() .UseKestrel() .UseUrls("https://0.0.0.0:5001") -
Defining
server.urlssetting 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:5000orhttps://*:5000;https://0.0.0.0:5000because 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();
}