【问题标题】:Configurable port number when using Kestrel?使用 Kestrel 时可配置端口号?
【发布时间】:2016-09-19 19:52:06
【问题描述】:

我已完成以下操作,但仍然无法正常工作。运行dotnet myapp.dll 仍然显示它正在监听http://localhost:5000

  1. 创建 hosting.json

代码:

{
  "server.url": "http://*:5001"
}
  1. 更新了 Program.cs

代码:

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

        var host = new WebHostBuilder()
            .UseConfiguration(config) // added
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            //.UseUrls("http://*:5001")
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}
  1. 更新了 project.json

代码:

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config",
      "NLog.config",
      "hosting.json"
    ]

【问题讨论】:

    标签: c# asp.net-core .net-core kestrel-http-server


    【解决方案1】:
    1. 你需要改变顺序:.SetBasePath应该在文件读取之前被调用

      var config = new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("hosting.json", optional: true, reloadOnChange: true)
          .Build();
      
    2. 使用server.urls,而不是server.url

    【讨论】:

    • 更改顺序后仍然显示监听5000。我会更新问题。
    • @dc7a9163d9 你也有印刷错误。使用“server.urls”,而不是“server.url”
    猜你喜欢
    • 1970-01-01
    • 2017-12-25
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多