When using WebHostBuilder
in a Main
entry-point, how can I specify the port it binds to?
By default it uses 5000.
Note that this question is specific to the new ASP.NET Core API (currently in 1.0.0-RC2).
In ASP.NET Core 3.1, there are four main ways to specify a custom port:
Using command line arguments, by starting your .NET application with --urls=[url]
:
dotnet run --urls=http://localhost:5001/
Using appsettings.json
, by adding a Urls
node:
{ "Urls": "http://localhost:5001" }
Using environment variables, with ASPNETCORE_URLS=http://localhost:5001/
.
Using UseUrls()
, if you prefer doing it programmatically:
public static class Program { public static void Main(string[] args) => CreateHostBuilder(args).Build().Run(); public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(builder => { builder.UseStartup<Startup>(); builder.UseUrls("http://localhost:5001/"); }); }
Or, if you're still using the web host builder instead of the generic host builder:
public class Program { public static void Main(string[] args) => new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseUrls("http://localhost:5001/") .Build() .Run(); }
UseUrls
is the approach recommended by the ASP.NET team for self-hosting scenarios (the value itself doesn't have to be hardcoded, obviously). That said, I updated my answer to mention how you could do that using the configuration builder.CommentedMay 21, 2016 at 16:32hosting.json
. The only thing, which one have to do is forcing reading the information in RC2 (see the announcement).using Microsoft.Extensions.Configuration.CommandLine;
You can insert Kestrel section in asp.net core 2.1+ appsettings.json file.
"Kestrel": { "EndPoints": { "Http": { "Url": "http://0.0.0.0:5002" } } },
if you do not have kestrel section, you can use "urls":
{ "urls":"http://*.6001;https://*.6002" }
but if you have kestrel in appsettings.json, urls section will failure.
dotnet publish
.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.
You'll need the following in your Dockerfile:
ENV ASPNETCORE_URLS=http://+:8080 EXPOSE 8080
and you'll need to modify the port in docker-compose.yml as well:
ports: - "8080"
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:
{ "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:
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://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments
[ProjectRoot]/Properties/launchSettings.json
, but the beautiful thing is, it works without a hitch.CommentedJan 20, 2019 at 3:43dotnet run
you need access to the source code.Above .net core 2.2 the method Main support args with WebHost.CreateDefaultBuilder(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>(); }
You can build your project and go to bin
run command like this
dotnet <yours>.dll --urls=http://0.0.0.0:5001
or with multi-urls
dotnet <yours>.dll --urls="http://0.0.0.0:5001;https://0.0.0.0:5002"
Edit 2021/09/14
After .net core 3.1 you can change the file appsettings.json
in the project, Config section Urls
and Kestrel
all works. And you can use either. Urls
will easier.
{ "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": "*" }
Use http://0.0.0.0:5000
can access the webserver from remote connect, If you set to http://localhost:5000
that will access only in your computer.
To make Kestrel
setting works, you shoud change code in Program.cs
in the project.
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>(); });
,
to a semi-colon like this: --urls="http://localhost:5001;https://localhost:5002"
. Otherwise, had an error starting Kestrel: System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase().
CommentedSep 13, 2021 at 15:36Alternative solution is to use a hosting.json
in the root of the project.
{ "urls": "http://localhost:60000" }
And then in 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(); }
urls
- thanks @ManishJainIf using dotnet run
dotnet run --urls="http://localhost:5001"
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.
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(); } }
On .Net Core 3.1 just follow Microsoft Doc that it is pretty simple: kestrel-aspnetcore-3.1
To summarize:
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
Go to properties/launchSettings.json and find your appname and under this, find applicationUrl. you will see, it is running localhost:5000, change it to whatever you want. and then run dotnet run...... hurrah
Alternatively, you can specify port by running app via command line.
Simply run command:
dotnet run --server.urls http://localhost:5001
Note: Where 5001 is the port you want to run on.
I found this question relating to .Net Core 3.1 but try to do it with .Net 6. So the first two options still work, but the programatical way has changed. (And that was what I needed).
So with .Net 6 you can use command line arguments:
dotnet run --urls=http://localhost:5001/
Using appsettings.json:
{ "Urls": "http://*:14071" }
Programmatically you should now:
var builder = WebApplication.CreateBuilder(options); //... builder.WebHost.UseUrls("http://localhost:14071");
It is also possible to use ConfigureKestrel with server options to define the Port. What gives you the option to define more details for Kestrel Options.
builder.WebHost.ConfigureKestrel(serverOptions => { serverOptions.ListenAnyIP(14072); });
or
var ipEndpoint = new IPEndPoint(IPAddress.Any, 14072); builder.WebHost.ConfigureKestrel(serverOptions => { serverOptions.Listen(ipEndpoint, listenOptions => { listenOptions.UseHttps("Your.pfx"); listenOptions.Use(async (context, next) => { // Your Middleware for e.g. logging }); });
I fixed the port issue in Net core 3.1 by using the following
In the Program.cs
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHost(x => x.UseUrls("https://localhost:4000", "http://localhost:4001")) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
You can access the application using
http://localhost:4000 https://localhost:4001
Solved multiple ports for angular with the server side dotnet cli using the --urls
like this:
dotnet run --urls="https://localhost:7203;http://localhost:5001;"
The order matters.
launchSettings.json
fromProperties
folder. You can change the port in thelaunchUrl
.hosting.json
(see the answer), which were used by default in RC1 and one need just add.AddJsonFile("hosting.json", optional: true)
(see here)hosting.json
or the command line for configuring of the binding.