Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the port an ASP.NET Core application is hosted on?

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).

like image 983
Drew Noakes Avatar asked May 21 '16 16:05

Drew Noakes


People also ask

How do I change the port number of a .NET Core application?

To change the port the application is using, Open the file lunchSetting. json. You will find it under the properties folder in your project and as shown below. Inside the file, change applicationUrl port (below is set to 5000) to a different port number and save the file.

Where is the host for ASP.NET Core web application is configured?

Answer is "Program.


1 Answers

In ASP.NET Core 3.1, there are 4 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(); } 
like image 72
Kévin Chalet Avatar answered Sep 20 '22 15:09

Kévin Chalet