Using appsettings.json to configure Kestrel listen port Dotnet core 2 preview 2

前端 未结 5 1178
别跟我提以往
别跟我提以往 2020-12-08 10:07

From what I understand the correct way of setting listen ports for ASP Dotnet Core 2 preview 1/2 is by creating a Kestrel section in the appsettings.json in the following fo

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 10:43

    As mentioned in a comment on the accepted answer, 2.1 has support for appsettings.json, see https://blogs.msdn.microsoft.com/webdev/2018/02/02/asp-net-core-2-1-roadmap/#security

    A working appsettings.json:

    "Kestrel": {
      "EndPoints": {
        "Http": {
          "Url": "http://localhost:5555"
        }
      }
    }
    

    This is for a Program.cs using (created by "dotnet new webapi"):

    WebHost.CreateDefaultBuilder(args)
    

    Relevant source code in github https://github.com/aspnet/MetaPackages/blob/master/src/Microsoft.AspNetCore/WebHost.cs#L163

    options.Configure(builderContext.Configuration.GetSection("Kestrel"));
    

    and https://github.com/aspnet/MetaPackages/blob/master/src/Microsoft.AspNetCore/WebHost.cs#L169

    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    

提交回复
热议问题