記錄一下已經實踐過的4種監聽url的方法:
一、 直接寫死url地址在代碼(不推薦使用這種)
webBuilder.UseUrls("http://192.168.1.1:7001;https://192.168.1.1:7002"); //或下面這種監聽本地所有的IP的埠 //webBuilder.UseUrls("http://*:7001");
二、使用dotnet 命令直接將地址通過main方法的args引數傳入
dotnet xxxx.dll --urls "http://127.0.0.1:7001;https://127.0.0.1:7002"

三、使用組態檔
新建一個hosting.json檔案,添加如下內容
{ "urls": "http://localhost:7001;http://localhost:7002" }
使用ConfigureWebHostDefaults加載組態檔,并使用組態檔中的urls屬性的value作為監聽地址
//加載組態檔 IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("hosting.json", optional: true).Build(); //設定監聽組態檔 webBuilder.UseConfiguration(config); //也可以,兩者等價 //webBuilder.UseUrls(config["urls"]);
為什么說兩者等價呢?
因為UseConfiguration()做的事情和UseUrls()的事情都是一樣的,



四、使用環境變數
新建變數名:ASPNETCORE_URLS,變數值:http://127.0.0.1:7001;https://127.0.0.1:7002 的環境變數

直接啟動已經編譯好的exe檔案或者使用dotnet xxx.dll檔案,就能監聽到環境變數設定的URL
參考:
https://www.cnblogs.com/huangxincheng/p/9569133.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/132200.html
標籤:.NET技术
上一篇:怎樣用VB.NET 實作Excel中的bubblechart,氣泡圖?
下一篇:節點操作
