指定AspNetCore运行地址的方法

How to specify the port an ASP.NET Core application is hosted on?

In ASP.NET Core 3.1, there are 4 main ways to specify a custom port:

Using command line arguments, by starting your .NET application with –urls=[url]:

1
dotnet run --urls=http://localhost:5001/

Using appsettings.json, by adding a Urls node:

1
2
3
{
"Urls": "http://localhost:5001"
}

Using environment variables, with ASPNETCORE_URLS=http://localhost:5001/.

Using UseUrls(), if you prefer doing it programmatically:

1
2
3
4
5
6
7
8
9
10
11
12
13
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:

1
2
3
4
5
6
7
8
9
10
11
12
public class Program
{
public static void Main(string[] args) =>
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://localhost:5001/")
.Build()
.Run();
}

指定AspNetCore运行地址的方法
https://pygyme.com/NET开发/指定aspnetcore运行地址的方法/
作者
PYGYME
发布于
2020年12月15日
许可协议