如何指定托管 ASP.NET Core 应用程序的端口? [英] How to specify the port an ASP.NET Core application is hosted on?
本文介绍了如何指定托管 ASP.NET Core 应用程序的端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Main
入口点中使用 WebHostBuilder
时,如何指定它绑定到的端口?
When using WebHostBuilder
in a Main
entry-point, how can I specify the port it binds to?
默认使用 5000.
请注意,此问题特定于新的 ASP.NET Core API(目前在 1.0.0-RC2 中).
Note that this question is specific to the new ASP.NET Core API (currently in 1.0.0-RC2).
推荐答案
在 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 应用程序:
- Using command line arguments, by starting your .NET application with
--urls=[url]
:
dotnet run --urls=http://localhost:5001/
- 使用
appsettings.json
,添加一个Urls
节点: - Using
appsettings.json
, by adding aUrls
node:
{
"Urls": "http://localhost:5001"
}
- 使用环境变量,
ASPNETCORE_URLS=http://localhost:5001/
. - 使用
UseUrls()
,如果您更喜欢以编程方式执行此操作: - Using environment variables, with
ASPNETCORE_URLS=http://localhost:5001/
. - Using
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();
}
这篇关于如何指定托管 ASP.NET Core 应用程序的端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文