想必看到这个文章的人都知道.net core 发布默认的是5000端口,更改端口的方法,网络上已经有的几种如下:
1、在program.cs 文件里写:
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>() .UseUrls("http://*:8080");
}2、写一个host.json
自己定义一个Configuration,然后修改的步骤如下:
1). 新增一个host.json,名字随便定义,自己看得懂就行啦。
{
"urls": "http://*:8080"
}2)webhost更改
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("host.json")
.Build();
return WebHost.CreateDefaultBuilder(args).UseConfiguration(configuration)
.UseStartup<Startup>();
}3、在launchSettings.json 使用环境变量配置
加环境变量:ASPNETCORE_URLS,如下代码:
"PKD.Test": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://*:8080"
}
}4、用Docker实现
这个看着更复杂,这里不说了。网上一搜一片
5、我的最终解决方案
以上是常见的解决方案,感觉都不太好,今天看微软的官方关于为 ASP.NET Core Kestrel Web 服务器配置终结点 | Microsoft Docs的文章,发现了个之前没见过的方法,就是一切都不用动,只需要在 appsettings.json里加上此代码就行:
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:8080"
} ,
"Https": {
"Url": "https://*:8081",
"Certificate": {
"Path": "<path to .pfx file>",
"Password": "<certificate password>"
}
}
}
}http 和 https 都支持配置,https使用的ssl证书也支持配置。这样就能实现我想要的了。而且感觉比较优雅,尤其是配置https方便。