更新时间:2022-11-05 18:20:58
在 ASP.NET Core 3.1 中,有 4 种主要的方式来指定自定义端口:
In ASP.NET Core 3.1, there are 4 main ways to specify a custom port:
--urls=[url]
启动您的 .NET 应用程序:--urls=[url]
:dotnet run --urls=http://localhost:5001/
appsettings.json
,添加一个 Urls
节点:
appsettings.json
, by adding a Urls
node:{
"Urls": "http://localhost:5001"
}
ASPNETCORE_URLS=http://localhost:5001/
.UseUrls()
,如果您更喜欢以编程方式执行此操作:
ASPNETCORE_URLS=http://localhost:5001/
.UseUrls()
, if you prefer doing it programmatically:public static class Program
{
public static void Main(string[] args) =>
CreateHostBuilder(args).Build().Run();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
builder.UseUrls("http://localhost:5001/");
});
}
或者,如果您仍在使用网络主机构建器而不是通用主机构建器:
Or, if you're still using the web host builder instead of the generic host builder:
public class Program
{
public static void Main(string[] args) =>
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://localhost:5001/")
.Build()
.Run();
}