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

17,719

Solution 1

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

Solution 2

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();
        }
Share:
17,719
F Andrei
Author by

F Andrei

Updated on June 15, 2022

Comments

  • F Andrei
    F Andrei almost 2 years

    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"
          }
        }
      }
    }
    
  • Zac
    Zac over 7 years
    Alternatively, chain on .UseUrls("http://localhost:5002") after instantiating the WebHostBuilder
  • Stefan Crain
    Stefan Crain about 6 years
    While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please edit to provide example code to show what you mean. Alternatively, consider writing this as a comment instead.