14

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"
      }
    }
  }
}
5
  • Did you try changing the url via project properties, rather than editing the launcher.json? I'd guess VS does update the IIS express web.config files with the url when you do it via the UI but doesn't when you run change it in the json Commented Jul 11, 2016 at 20:13
  • 1
    There are two places where the IISExpress ports are configured. First one is %userprofile%\Documents\IISExpress\config in the applicationhost.config. The other one is located solution-wide configuration in the .vs/config folder, same filename. This ones determine the port number used when IIS Express launches the application Commented Jul 11, 2016 at 20:17
  • benfoster.io/blog/… check this may help you Commented Jan 14, 2017 at 13:29
  • 4
    It's incredible that 2 years later this still doesn't work!!! Commented Mar 22, 2019 at 3:25
  • You're trying to bind a port < 1024 as a regular user. Maybe it's defaulting to 5000? Commented Jan 22, 2020 at 19:17

4 Answers 4

6

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: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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

1 Comment

Alternatively, chain on .UseUrls("http://localhost:5002") after instantiating the WebHostBuilder
0

If you want this button working again with the ports you want

enter image description here

delete bin/Debug within your project folder and be happy

Comments

-2

I encountered similar problem, changing applicationUrl in launchSettings.json not work .

finally I founded that it is override by this item in appsettings.json or appsettings.Development.json

   "urls": "https://localhost:5101",

2 Comments

This is not really a helpful answer. Please consider the answers in this post with multiple upvotes as well written and good effort answers. They all provide a context and explanation of the solution.
@Ebbelink i encountered this kind of problem and fixed it. i think it's you who not understand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.