asp.net-core红est。配置监听地址

示例

使用Kestrel,您可以使用以下方法指定端口:

  1. 定义ASPNETCORE_URLS环境变量。

    视窗

    SET ASPNETCORE_URLS=https://0.0.0.0:5001

    OS X

    export ASPNETCORE_URLS=https://0.0.0.0:5001
  2. 通过命令行传递--server.urls参数

    dotnet run --server.urls=http://0.0.0.0:5001
  3. 使用UseUrls()方法

    var builder = new WebHostBuilder()
                 .UseKestrel()
                 .UseUrls("http://0.0.0.0:5001")
  4. server.urls在配置源中定义设置。

hosting.json例如,下一个样本使用文件。

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

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

可能值的示例:

  • 在任何接口上的任何IP4和IP6地址上侦听5000:

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

    要么

     "server.urls": "http://::5000;http://0.0.0.0:5000"
  • 在每个IP4地址上侦听5000:

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

一要仔细,不能使用http://*:5000;http://::5000, http://::5000;http://*:5000,http://*:5000;http://0.0.0.0:5000或http://*:5000;http://0.0.0.0:5000因为它需要注册IP6地址::或IP4地址0.0.0.0两次

将文件添加到publishOptions在project.json

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

并且在创建WebHostBuilder时在应用程序调用的入口点:.UseConfiguration(config)

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();
}