ASP.NET Core 6(.NET 6) 默认将 HTTP 端口绑定到 5000,将 HTTPS 端口绑定到 5001。可以通过以下三种方式修改默认端口:
详细文档:ASP.NET Core 6(.NET 6) 修改默认端口的方法(5000和5001)-CJavaPy
方法一:修改 launchSettings.json 文件
在项目的根目录下,找到 launchSettings.json
文件。在文件中,找到 <environment name="Development">
部分,并修改 <webHostUrl>
属性的值。
例如,要将 HTTP 端口修改为 80,请将 <webHostUrl>
属性的值修改为 http://localhost:80
。
JSON
{
"profiles": {
"ASPNETCore": {
"commandName": "dotnet",
"launchBrowser": true,
"environments": {
"Development": {
"commandArgs": ["run", "--urls", "http://localhost:80"],
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
}
}
方法二:修改 appsettings.json 文件
在项目的根目录下,找到 appsettings.json
文件。在文件中,找到 Kestrel
部分,并修改 Endpoints
属性的值。
例如,要将 HTTP 端口修改为 80,请将 Endpoints
属性的值修改为 http://localhost:80
。
JSON
{
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:80"
}
}
}
}
方法三:在代码中修改
在 Program.cs 文件中,找到 builder.WebHost()
方法。在方法中,修改 urls
属性的值。
例如,要将 HTTP 端口修改为 80,请将 urls
属性的值修改为 http://localhost:80
。
C#
public static void Main(string[] args)
{
var builder = WebHost.CreateDefaultBuilder(args);
// 修改 HTTP 端口
builder.WebHost.Endpoints.DefaultHost.Addresses.Add("http://localhost:80");
builder.Build().Run();
}