記錄一下已經實踐過的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