Search code examples
visual-studioasp.net-core-2.0kestrel-http-server

How to launch an url on F5 when using Kestrel on a specific port?


I have an Asp.Net Core 2.2 application using Kestrel with default settings. I went in the project's debug properties and set the "Launch Browser" setting to the page I want to start with when debugging and "Launch" to "Project". This all works fine but I want Kestrel to use a specific port. I found plenty of example that work for the port (I use the hosting.json way) but all of them seem to disregard the "Launch Browser" setting.

Is there no way to have Visual Studio automatically open a new window/tab with my chosen URL and use a specific port when I debug?

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        var host = WebHost.CreateDefaultBuilder(args)
                    .UseKestrel()
                    .UseStartup<Startup>()
                    .Build();
        host.Run();
    }
}

launchSettings.json

{
  "profiles": {
    "Kestrel": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger" 
    }
  }
}

hosting.json

{
  "urls": "https://localhost:44350/;"
}

and if I'm using hosting.json, my main is:

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddCommandLine(args)
        .AddJsonFile("hosting.json", optional: true)
        .Build();

    var host = WebHost.CreateDefaultBuilder(args)
                .UseConfiguration(config)
                .UseKestrel()
                .UseStartup<Startup>()
                .Build();

    host.Run();
}


Solution

  • In the project's debug properties , you should set the App URL of "Web Server Settings" to the specific port you want , and the "Launch Browser" is default checked.

    Or you chould also set the specific port in the launchSettings.json like below:

    "MVC2_2Project": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:7001;http://localhost:7000"
    }
    

    The setting in launchSettings.json and the project's debug properties is synchronous, you can set it up in one place.