【问题标题】:How to launch an url on F5 when using Kestrel on a specific port?在特定端口上使用 Kestrel 时如何在 F5 上启动 url?
【发布时间】:2019-02-07 22:03:10
【问题描述】:

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

有没有办法让 Visual Studio 自动打开一个带有我选择的 URL 的新窗口/选项卡并且在我调试时使用特定端口?

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

【问题讨论】:

    标签: visual-studio asp.net-core-2.0 kestrel-http-server


    【解决方案1】:

    在项目的调试属性中,将“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中的设置和项目的调试属性是同步的,你可以在一处设置。

    【讨论】:

    • 我忘了回来回答自己,但我最终这样做了,所以我会接受这个。
    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2021-12-12
    • 2018-05-10
    相关资源
    最近更新 更多