.netasp.net-core

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


Solution

  • In ASP.NET Core 3.1, there are four main ways to specify a custom port:

    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();
    }