Asp.net-Core

我可以在 ASP.net Core 2.0 Preview 中的 appsettings.json 中設置監聽 URL 嗎?

  • July 28, 2020

我正在創建一個 ASP.net Core 2.0 應用程序以在 .net Core 2.0 執行時上執行,目前兩者都處於預覽版中。但是,我無法弄清楚如何讓 Kestrel 使用預設http://localhost:5000偵聽 URL 以外的其他內容。

我可以用Google搜尋的大多數文件都在談論一個server.urls設置,即使在 1.0-preview 中似乎也已更改為 just urls,但是兩者都不起作用(打開調試日誌記錄讓 Kestrel 告訴我沒有配置監聽端點)。

很多文件還談到了hosting.json我不能使用預設的 appsettings.json。但是,如果我比較推薦的載入新配置的方法,這看起來與新WebHost.CreateDefaultBuilder方法的作用非常相似,只是它載入了 appsettings.json。

我目前不了解 appsettings.json 和IConfigureOptions<T>它們之間的關係(如果有的話),所以我的麻煩可能源於對KestrelServerOptionsSetup實際操作缺乏了解。

我得到了它的工作

public static IWebHost BuildWebHost(string[] args) => 
       WebHost.CreateDefaultBuilder(args)
           .UseConfiguration(new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("hosting.json", optional: true)
               .Build()
           )
           .UseStartup<Startup>()
           .Build();

和hosting.json

{ "urls": "http://*:5005;" }

要在 appsettings.json 中設置監聽 URL,請添加“Kestrel”部分:

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

參考:https ://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2

引用自:https://stackoverflow.com/questions/44117840