当在主入口点使用WebHostBuilder时,我如何指定它绑定到的端口?
默认情况下,它使用5000。
请注意,这个问题是特定于新的ASP。NET Core API(目前在1.0.0-RC2中)。
当在主入口点使用WebHostBuilder时,我如何指定它绑定到的端口?
默认情况下,它使用5000。
请注意,这个问题是特定于新的ASP。NET Core API(目前在1.0.0-RC2中)。
当前回答
以上。net core 2.2方法Main使用WebHost.CreateDefaultBuilder(args)支持args
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
您可以构建您的项目并像这样进入bin运行命令
dotnet <yours>.dll --urls=http://0.0.0.0:5001
或者使用多url
dotnet <yours>.dll --urls="http://0.0.0.0:5001;https://0.0.0.0:5002"
编辑2021/09/14
在。net core 3.1之后,你可以修改文件appsettings。Config section Urls和Kestrel都可以工作。你可以用任何一种。url会更简单。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"MicrosoftHostingLifetime": "Information"
}
},
"Urls": "http://0.0.0.0:5002",
//"Kestrel": {
// "EndPoints": {
// "Http": {
// "Url": "http://0.0.0.0:5000"
// },
// "Https": {
// "Url": "https://0.0.0.0:5001"
// }
// }
//},
"AllowedHosts": "*"
}
使用http://0.0.0.0:5000可以从远程连接访问web服务器,如果你设置为http://localhost:5000,则只能在你的计算机中访问。
要使Kestrel设置工作,您应该更改项目中的Program.cs中的代码。
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices((context, services) =>
{
services.Configure<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>(context.Configuration.GetSection("Kestrel"));
});
webBuilder.UseStartup<Startup>();
});
其他回答
在。net Core 3.1上,只需遵循微软的文档,它非常简单:kestrel-aspnetcore-3.1
总结:
Add the below ConfigureServices section to CreateDefaultBuilder on Program.cs: // using Microsoft.Extensions.DependencyInjection; public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { services.Configure<KestrelServerOptions>( context.Configuration.GetSection("Kestrel")); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); Add the below basic config to appsettings.json file (more config options on Microsoft article): "Kestrel": { "EndPoints": { "Http": { "Url": "http://0.0.0.0:5002" } } } Open CMD or Console on your project Publish/Debug/Release binaries folder and run: dotnet YourProject.dll Enjoy exploring your site/api at your http://localhost:5002
你可以在asp.net core 2.1+ appsettings中插入Kestrel section。json文件。
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:5002"
}
}
},
如果你没有kestrel部分,你可以使用"urls":
{
"urls":"http://*.6001;https://*.6002"
}
但如果你在appsettings中有kestrel。Json, url部分将失败。
另一种解决方案是使用主机。Json在项目的根目录中。
{
"urls": "http://localhost:60000"
}
然后在program。cs中
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", true)
.Build();
var host = new WebHostBuilder()
.UseKestrel(options => options.AddServerHeader = false)
.UseConfiguration(config)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
或者,你也可以通过命令行运行app来指定端口。
简单地运行命令:
dotnet run --server.urls http://localhost:5001
注意:其中5001是要运行的端口。
以上。net core 2.2方法Main使用WebHost.CreateDefaultBuilder(args)支持args
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
您可以构建您的项目并像这样进入bin运行命令
dotnet <yours>.dll --urls=http://0.0.0.0:5001
或者使用多url
dotnet <yours>.dll --urls="http://0.0.0.0:5001;https://0.0.0.0:5002"
编辑2021/09/14
在。net core 3.1之后,你可以修改文件appsettings。Config section Urls和Kestrel都可以工作。你可以用任何一种。url会更简单。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"MicrosoftHostingLifetime": "Information"
}
},
"Urls": "http://0.0.0.0:5002",
//"Kestrel": {
// "EndPoints": {
// "Http": {
// "Url": "http://0.0.0.0:5000"
// },
// "Https": {
// "Url": "https://0.0.0.0:5001"
// }
// }
//},
"AllowedHosts": "*"
}
使用http://0.0.0.0:5000可以从远程连接访问web服务器,如果你设置为http://localhost:5000,则只能在你的计算机中访问。
要使Kestrel设置工作,您应该更改项目中的Program.cs中的代码。
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices((context, services) =>
{
services.Configure<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>(context.Configuration.GetSection("Kestrel"));
});
webBuilder.UseStartup<Startup>();
});