When using WebHostBuilder
in a Main
entry-point, how can I specify the port it binds to?
在Main入口点使用WebHostBuilder时,如何指定它绑定的端口?
By default it uses 5000.
默认情况下,它使用5000。
Note that this question is specific to the new ASP.NET Core API (currently in 1.0.0-RC2).
请注意,此问题特定于新的ASP.NET Core API(目前在1.0.0-RC2中)。
5 个解决方案
#1
84
You can use UseUrls
for that:
您可以使用UseUrls:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://localhost:5001/")
.Build();
host.Run();
}
}
Alternatively, you can configure the server address using the configuration stack. Here's how you could use the command line arguments to flow the address you want:
或者,您可以使用配置堆栈配置服务器地址。以下是如何使用命令行参数来传递所需的地址:
public class Program
{
public static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseConfiguration(configuration)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
dotnet run --server.urls=http://localhost:5001/
#2
22
Follow up answer to help anyone doing this with the VS docker integration. I needed to change to port 8080 to run using the "flexible" environment in google appengine.
通过VS docker集成帮助任何人这样做的后续答案。我需要更改到端口8080以使用google appengine中的“灵活”环境运行。
You'll need the following in your Dockerfile:
您需要在Dockerfile中使用以下内容:
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
and you'll need to modify the port in docker-compose.yml as well:
你还需要修改docker-compose.yml中的端口:
ports:
- "8080"
#3
20
Alternative solution is to use a hosting.json
in the root of the project.
替代解决方案是在项目的根目录中使用hosting.json。
{
"urls": "http://localhost:60000"
}
And then in Program.cs
然后在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();
}
#4
7
You can specify hosting URL without any changes to your app.
您可以指定托管网址,而无需对应用进行任何更改。
Create a Properties/launchSettings.json
file in your project directory and fill it with something like this:
在项目目录中创建一个Properties / launchSettings.json文件,并用以下内容填充:
{
"profiles": {
"MyApp1-Dev": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5001/"
}
}
}
dotnet run
command should pick your launchSettings.json
file and will display it in the console:
dotnet run命令应该选择你的launchSettings.json文件并将其显示在控制台中:
C:\ProjectPath [master ≡]
λ dotnet run
Using launch settings from C:\ProjectPath\Properties\launchSettings.json...
Hosting environment: Development
Content root path: C:\ProjectPath
Now listening on: http://localhost:5001
Application started. Press Ctrl+C to shut down.
More details: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments
更多细节:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments
#5
4
When hosted in docker containers (linux version for me), you might get a 'Connection Refused' message. In that case you can use IP address 0.0.0.0 which means "all IP addresses on this machine" instead of the localhost loopback to fix the port forwarding.
当托管在容器(我的Linux版本)中时,您可能会收到“拒绝连接”消息。在这种情况下,您可以使用IP地址0.0.0.0,这意味着“此计算机上的所有IP地址”而不是localhost环回来修复端口转发。
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://0.0.0.0:5000/")
.Build();
host.Run();
}
}
#1
84
You can use UseUrls
for that:
您可以使用UseUrls:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://localhost:5001/")
.Build();
host.Run();
}
}
Alternatively, you can configure the server address using the configuration stack. Here's how you could use the command line arguments to flow the address you want:
或者,您可以使用配置堆栈配置服务器地址。以下是如何使用命令行参数来传递所需的地址:
public class Program
{
public static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseConfiguration(configuration)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
dotnet run --server.urls=http://localhost:5001/
#2
22
Follow up answer to help anyone doing this with the VS docker integration. I needed to change to port 8080 to run using the "flexible" environment in google appengine.
通过VS docker集成帮助任何人这样做的后续答案。我需要更改到端口8080以使用google appengine中的“灵活”环境运行。
You'll need the following in your Dockerfile:
您需要在Dockerfile中使用以下内容:
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
and you'll need to modify the port in docker-compose.yml as well:
你还需要修改docker-compose.yml中的端口:
ports:
- "8080"
#3
20
Alternative solution is to use a hosting.json
in the root of the project.
替代解决方案是在项目的根目录中使用hosting.json。
{
"urls": "http://localhost:60000"
}
And then in Program.cs
然后在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();
}
#4
7
You can specify hosting URL without any changes to your app.
您可以指定托管网址,而无需对应用进行任何更改。
Create a Properties/launchSettings.json
file in your project directory and fill it with something like this:
在项目目录中创建一个Properties / launchSettings.json文件,并用以下内容填充:
{
"profiles": {
"MyApp1-Dev": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5001/"
}
}
}
dotnet run
command should pick your launchSettings.json
file and will display it in the console:
dotnet run命令应该选择你的launchSettings.json文件并将其显示在控制台中:
C:\ProjectPath [master ≡]
λ dotnet run
Using launch settings from C:\ProjectPath\Properties\launchSettings.json...
Hosting environment: Development
Content root path: C:\ProjectPath
Now listening on: http://localhost:5001
Application started. Press Ctrl+C to shut down.
More details: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments
更多细节:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments
#5
4
When hosted in docker containers (linux version for me), you might get a 'Connection Refused' message. In that case you can use IP address 0.0.0.0 which means "all IP addresses on this machine" instead of the localhost loopback to fix the port forwarding.
当托管在容器(我的Linux版本)中时,您可能会收到“拒绝连接”消息。在这种情况下,您可以使用IP地址0.0.0.0,这意味着“此计算机上的所有IP地址”而不是localhost环回来修复端口转发。
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://0.0.0.0:5000/")
.Build();
host.Run();
}
}