【问题标题】:Asp.Net Core change url in launchSettings.json not workingLaunchSettings.json 中的 Asp.Net Core 更改 url 不起作用
【发布时间】:2016-07-11 19:23:54
【问题描述】:

当我将网站作为控制台应用程序运行时,我想更改默认 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"
      }
    }
  }
}

【问题讨论】:

  • 您是否尝试通过项目属性更改 url,而不是编辑 launcher.json?我猜VS确实会在您通过UI执行此操作时使用url更新IIS express web.config文件,但在您运行时不会在json中更改它
  • 有两个地方配置了 IISExpress 端口。第一个是applicationhost.config 中的%userprofile%\Documents\IISExpress\config。另一个位于.vs/config 文件夹中的解决方案范围配置,文件名相同。这些决定了 IIS Express 启动应用程序时使用的端口号
  • benfoster.io/blog/… 检查这可能对你有帮助
  • 令人难以置信的是 2 年后这仍然不起作用!!!
  • 您正在尝试将

标签: asp.net asp.net-mvc asp.net-core asp.net-core-mvc


【解决方案1】:

您需要在构建“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();


}

Screenshot of the console output

【讨论】:

  • 虽然这可能是解决问题的宝贵提示,但一个好的答案也可以证明解决方案。请编辑以提供示例代码以说明您的意思。或者,考虑将其写为评论。
【解决方案2】:

使用 Kestrel,您可以使用 hosting.json 文件指定端口。

将包含以下内容的hosting.json添加到您的项目中:

{
    "server.urls": "http://0.0.0.0:5002" 
}

并添加到 project.json 中的 publishOptions

"publishOptions": {
"include": [
  "hosting.json"
  ]
}

并在创建 WebHostBuilder 时在应用程序调用“.UseConfiguration(config)”的入口点:

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

            var host = new WebHostBuilder()
                .UseConfiguration(config)
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }

【讨论】:

  • 或者,在实例化 WebHostBuilder 后链接 .UseUrls("http://localhost:5002")
猜你喜欢
  • 2021-10-01
  • 2018-10-21
  • 2016-08-19
  • 2019-09-23
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
相关资源
最近更新 更多