asp.net Core 自定義端口
官方文檔
- aspnet內庫源碼: https://github.com/aspnet
- dotnet系統內庫源碼:https://github.com/dotnet
- asp.net core 官方文檔
自定義端口訪問
- webHost增加UseUrls。 例:WebHost.UseUrls("http://:5001","http://:5002");
- 配置文件 hosting.json。例:
通過查看WebHost源碼我們得知,啟動后會先讀取相關配置參數,
internal class WebHost:IWebHost
{
private static readonly string DeprecatedServerUrlsKey = "server.urls";
//...
private void EnsureServer()
{
if (Server == null)
{
//...
if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
{
var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey];
}
}
}
}
public static class WebHostDefaults{
public static readonly string ServerUrlsKey = "urls";
//...
}
{"server.urls": "http://localhost:5003;http://localhost:5004"}
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", true)
.Build();
BuildWebHost(args, config).Run();
//BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args, IConfiguration config) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
// .UseUrls("http://*:5001", "http://*:5002")
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
}
- 配置環境變量。設置ASPNETCORE_URLS、ASPNET_ENV、ASPNETCORE_SERVER.URLS的值。
Web服務器
- Kestrel(默認)
- HTTP.sys(在使用 IIS 的反向代理配置中不起作用)
- 自定義服務器
托管和部署
- linux
- centos7.2
- windows
- IIS
- windows服務