简体   繁体   中英

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.

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"
      }
    }
  }
}

Using Kestrel you can specify port using hosting.json file.

Add hosting.json with the following content to you project:

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

and add to publishOptions in project.json

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

and in entry point for the application call ".UseConfiguration(config)" when creating WebHostBuilder:

        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". Hope this one helps you https://github.com/aspnet/KestrelHttpServer/issues/639

Below is the code I use in my .net core 2.0 console application

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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