简体   繁体   English

Asp.Net Core 在 launchSettings.json 中更改 url 不起作用

[英]Asp.Net Core change url in launchSettings.json not working

I want to change the default url ( http://localhost:5000 ) when i run the website as a console application.当我将网站作为控制台应用程序运行时,我想更改默认值 url ( http://localhost:5000 )。

I edited launchSettings.json but it doesn't work... it still uses port 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"
      }
    }
  }
}

Using Kestrel you can specify port using hosting.json file. 使用Kestrel,您可以使用hosting.json文件指定端口。

Add hosting.json with the following content to you project: 将hosting.json添加到您的项目中以下内容:

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

and add to publishOptions in project.json 并添加到project.json中的publishOptions

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

and in entry point for the application call ".UseConfiguration(config)" when creating WebHostBuilder: 在创建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();
        }

这是一个已知的问题(我不能指出GitHub上的一个问题,因为它是一个私人回购)。

You need to add the url when you build the "BuildWebHost". 您需要在构建“BuildWebHost”时添加URL。 Hope this one helps you https://github.com/aspnet/KestrelHttpServer/issues/639 希望这个可以帮助你https://github.com/aspnet/KestrelHttpServer/issues/639

Below is the code I use in my .net core 2.0 console application 下面是我在.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 控制台输出的屏幕截图

If you want this button working again with the ports you want如果你想让这个按钮再次与你想要的端口一起工作

在此处输入图像描述

delete bin/Debug within your project folder and be happy删除项目文件夹中的bin/Debug并开心

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM