LaunchSettings.json中的Asp.Net Core更改URL不起作用

F A*_*rei 5 asp.net asp.net-mvc asp.net-core-mvc asp.net-core

当我将网站作为控制台应用程序运行时,我想更改默认URL(http:// localhost:5000)。

我编辑了launchSettings.json但它不起作用...它仍然使用端口5000:

    {
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:4230/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "website": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:80",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

您需要在构建“BuildWebHost”时添加 url。希望这对您有帮助https://github.com/aspnet/KestrelHttpServer/issues/639

下面是我在 .net core 2.0 控制台应用程序中使用的代码

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseUrls("http://localhost:5050/")
            .Build();
        
    
}
Run Code Online (Sandbox Code Playgroud)

控制台输出截图: 在此输入图像描述

  • 虽然这可能是解决问题的一个有价值的提示,但一个好的答案也可以展示解决方案。请编辑以提供示例代码来说明您的意思。或者,考虑将其写为评论。 (2认同)