NETCORE 設置監聽URL

記錄一下已經實踐過的4種監聽url的方法:html

 

1、 直接寫死url地址在代碼(不推薦使用這種)web

 webBuilder.UseUrls("http://192.168.1.1:7001;https://192.168.1.1:7002");
 //或下面這種監聽本地全部的IP的端口
 //webBuilder.UseUrls("http://*:7001");

 

2、使用dotnet 命令直接將地址經過main方法的args參數傳入json

dotnet xxxx.dll --urls "http://127.0.0.1:7001;https://127.0.0.1:7002"

 

3、使用配置文件ui

新建一個hosting.json文件,添加以下內容url

{
"urls": "http://localhost:7001;http://localhost:7002"
}

使用ConfigureWebHostDefaults加載配置文件,並使用配置文件中的urls屬性的value做爲監聽地址spa

//加載配置文件
IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("hosting.json", optional: true).Build();
//設置監聽配置文件
webBuilder.UseConfiguration(config);
//也能夠,二者等價
//webBuilder.UseUrls(config["urls"]);

 

爲何說二者等價呢?3d

由於UseConfiguration()作的事情和UseUrls()的事情都是同樣的。code

 

 

 

 

 

 

 

4、使用環境變量htm

新建變量名:ASPNETCORE_URLS,變量值:http://127.0.0.1:7001;https://127.0.0.1:7002 的環境變量blog

直接啓動已經編譯好的exe文件或者使用dotnet xxx.dll文件,就能監聽到環境變量設置的URL

  

 

參考:

http://www.javashuo.com/article/p-bsguttkb-ma.html

相關文章
相關標籤/搜索