前言
在使用asp.net core 3.1開發時,需要配置服務器監聽的端口和協議,官方幫助文檔進行簡單說明,文檔中提到了4種指定url的方法
-
設置
aspnetcore_urls
環境變量; -
使用
dotnet --urls
命令行參數; -
使用
urls
作為鍵進行配置; -
使用
useurls
擴展方法;
為便于講清楚urls設置方法,創建名為aspnetcoreurl
的asp.net core web api程序進行說明,默認情況下,啟動asp.net core后,監聽以下urls:
?http://localhost:5000/
?https://localhost:5001/
url格式
主要有三種url格式表達方式,可以查看官方文檔(https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1)
格式1:{scheme}://{loopbackaddress}:{port}
,例如http://localhost:5000/
、https://localhost:5001/
格式2:{scheme}://{ipaddress}:{port}
,例如http://127.0.0.1:5000/
、https://192.168.1.100:5001/
格式3:{scheme}://*:{port}
,例如http://*:5000/
、https://domain.com:5001/
前提條件
在開始測試幾種urls設置方法前,需要先生成aspnetcoreurl
程序,然后在生成的根目錄下打開命令行工具進行相應的測試
如圖,我這里的程序根目錄為d:\aspnetcoreurl\aspnetcoreurl\bin\debug\netcoreapp3.1
,使用的終端為微軟官方的windows powershell
方法1 使用環境變量
在不修改aspnetcoreurl
任何源代碼的情況下(即創建項目時的程序默認狀態)生成程序,定位到生成的根目錄下,打開命令行終端
1
2
3
4
5
6
|
# 環境變量僅在當前命令行窗口生效 $env:aspnetcore_urls = "http://localhost:7000;https://localhost:7010" # 或者使用dotnet_urls環境變量同樣可生效 $env:dotnet_urls = "http://localhost:8000;https://localhost:8010" # 運行aspnetcoreurl程序 dotnet aspnetcoreurl.dll |
如果使用windows命令行(即cmd命令行),使用下面的方式設置
1
2
3
4
5
6
7
8
|
# 環境變量僅在當前命令行窗口生效 set aspnetcore_urls=http: //localhost:7000;https://localhost:7010 # 將aspnetcore_urls變量保存到用戶環境變量中 setx aspnetcore_urls "http://localhost:7000;https://localhost:7010" # 加/m參數,將aspnetcore_urls變量保存到系統環境變量中 setx aspnetcore_urls "http://localhost:7000;https://localhost:7010" /m # 運行aspnetcoreurl程序 dotnet aspnetcoreurl.dll |
注意:使用setx設置環境變量后,需要打開新的windows命令行窗口才會使用環境變量生效
在linux系統中使用以下命令設置環境變量
1
2
|
# 環境變量僅在當前終端生效,關閉終端后需要重新設置 export aspnetcore_urls=http: //localhost:7000;https://localhost:7010 |
方法2 使用命令行參數
在生成程序的根目錄下,使用dotnet --urls
命令,并帶上urls參數
1
|
dotnet aspnetcoreurl.dll --urls http: //localhost:7001;https://localhost:7011 |
方法3 使用配置文件
在生成程序的根目錄下,打開appsettings.json
文件,添加url配置項,然后雙擊aspnetcoreurl.exe
運行
1
2
3
|
{ "urls" : "http://localhost:7002;http://localhost:7012" } |
運行結果如下圖
方法4 使用useurls
這種方法需要修改源代碼,打開program.cs
文件,修改createhostbuilder
方法內容,主要是添加useurls擴展方法然后生成程序。
1
2
3
4
5
6
7
8
|
public static ihostbuilder createhostbuilder( string [] args) => host.createdefaultbuilder(args) .configurewebhostdefaults(webbuilder => { webbuilder.usestartup<startup>(); // 使用useurls設置監聽的端口和協議 webbuilder.useurls( "http://localhost:7003" , "https://localhost:7013" ); }); |
注意:運行前需要將appsettings.json
文件恢復到默認狀態,即沒有配置urls的狀態,否則配置文件中設置會覆蓋代碼中的方法。
方法5 使用kestrel
這種方法并不在官方文檔中,因為該方法僅限于使用kestrel的時候生效,在創建asp.net core應用程序時,默認使用kestrel來托管應用,使用這種方法也有相應的應用場景,這里簡單介紹下該方法的使用
打開program.cs
文件,修改createhostbuilder
方法內容
1
2
3
4
5
6
7
8
9
10
11
12
|
public static ihostbuilder createhostbuilder( string [] args) => host.createdefaultbuilder(args) .configurewebhostdefaults(webbuilder => { webbuilder.usestartup<startup>(); // 配置kestrel服務 webbuilder.usekestrel(kestrelserveroptions => { kestrelserveroptions.listenlocalhost(7004); kestrelserveroptions.listenlocalhost(7014, listenoptions => listenoptions.usehttps()); }); }); |
優先級
前面介紹的5種方法都在獨立運行,如果將這5種方法同時用上,會有什么樣的效果呢,下面來測試下,為了便于測試,每種方法都使用了不同的端口進行區分,下面顯示了使用全部方法的運行結果:
可以看到,生效的是使用kestrel方法,現在將kestrel這種注釋掉,運行結果如下:
可以看到此時生效的是使用命令行參數方法,如果不使用命令行參數方法,運行結果如下:
生效的是配置文件中的設置,現在刪除配置文件中urls的配置,運行結果如下:
生效的是useurls擴展方法,現在注釋掉useurls方法代碼,運行結果如下:
生效的是環境變量,如果不設置環境變量,則使用默認值,即http://localhost:5000/
和https://localhost:5001/
。
總結
本文介紹了asp.net core幾種常用的設置urls的方法,大家可以根據項目實際情況選擇其中一種或幾種,如果同時使用幾種urls設置方法,則需要留意配置的優先級問題,經過測試得出
kestrel > 命令行 > 配置文件 > useurls > 環境變量 > 默認值
如果你想自己試一試,代碼地址:
gitee:https://gitee.com/gyhgis/aspnetcoreurl
github:https://github.com/gyhgis/aspnetcoreurl
到此這篇關于asp.net core設置urls的五種方法的文章就介紹到這了,更多相關asp.net core設置urls 內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/gyhgis/p/15517043.html