Asp.Net Core change url in launchSettings.json not working

时间:2016-07-11 19:23:55

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

I want to change the default url ( http://localhost:5000 ) when i run the website as a console application .

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

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

This is a known issue (I cannot point to an issue on GitHub as it was filed a private repo).

答案 2 :(得分: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