Kestrel 配置偵聽地址
使用 Kestrel,你可以使用下一種方法指定埠:
-
定義
ASPNETCORE_URLS
環境變數。視窗
SET ASPNETCORE_URLS=https://0.0.0.0:5001
OS X.
export ASPNETCORE_URLS=https://0.0.0.0:5001
-
通過命令列傳遞
--server.urls
引數dotnet run --server.urls=http://0.0.0.0:5001
-
使用
UseUrls()
方法var builder = new WebHostBuilder() .UseKestrel() .UseUrls("http://0.0.0.0:5001")
-
在配置源中定義
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
在 project.json
中新增檔案到 publishOptions
"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();
}