在特定端口上使用 Kestrel 时,如何在 F5 上启动 url?

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

我有一个 Asp.Net Core 2.2 应用程序,使用默认设置使用 Kestrel。我进入了项目的调试属性并将 "Launch Browser" 设置设置为调试时我想要开始的页面,并将 "Launch" 设置为 "Project"。这一切都很好,但我希望 Kestrel 使用特定端口。我发现了很多适用于该端口的示例(我使用 hosting.json 方式)但它们似乎都忽略了 "Launch Browser" 设置。

有没有办法让 Visual Studio 使用我选择的 URL 使用特定端口自动打开一个新的 window/tab调试?

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/;"
}

如果我使用 hosting.json,我的主要是:

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

在项目的调试属性中,您应该将"Web Server Settings"的App URL设置为您想要的特定端口,并且"Launch Browser"是默认选中的。

或者您也可以在 launchSettings.json 中设置特定端口,如下所示:

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

launchSettings.json中的设置和项目的调试属性是同步的,可以在一处设置。