5

I've edited launchSettings.JSON file and changed the port like so.

"Gdb.Blopp": {
  "commandName": "Project",
  "launchBrowser": false,
  "launchUrl": "http://localhost:4000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }

It still starts on port 5000, though. Is that setting disregarded all toghether or am I missing something else?

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • launchSettings.json is only for VIsual Studio, when you hit F5. But you should directly edit that file and rather use Project Properties to change stuff. Because if you change project properties it will also edit the IIS Express files which are located in the `.vs/config/applicationhost.config` if you want to change the port kestrel uses, use `.UseUrls("http://0.0.0.0:4000")` in `Program.cs`when you run IIS/IISExpress the kestrel port will be determined by `UseIISIntegration()` – Tseng Nov 11 '16 at 15:38
  • @Tseng Nice, that's what I started to suspect the last few minutes of googlearching. It seems so weird to set the port from compileable code and not a JSON setting file. Anyway, please post your comment as a reply so I can accept it. – Konrad Viltersten Nov 11 '16 at 15:50
  • Well you can also set it via config file or as commandline parameter, see my answer below. Was just out of the scope of a comment. For command – Tseng Nov 11 '16 at 16:58

2 Answers2

9

The launchSettings.json supposed to be used by IDEs (i.e. Visual Studio), when you hit F5/Ctr+F5 and offers the options from the pull-down menu next to the start button.

enter image description here

Also you shouldn't directly edit that the launcherSettings.json file and instead use the Project Properties to change stuff.

One reason for this is that if you change it via project properties, Visual Studio will also edit the IIS Express files (located in the .vs/config/applicationhost.config folder of your solution).

If you want to change the port kestrel uses, use .UseUrls("http://0.0.0.0:4000") (get it either from appsettings.json or hosting.json) in Program.cs.

If you don't want to use hardcoded, you can also do something like this

Create a hosting.json:

{
  "server": "Microsoft.AspNetCore.Server.Kestrel",
  "server.urls": "http://localhost:4000"
}

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("hosting.json", optional: false)
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")
            .Build();

        var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

You can also do that via commandline (AddCommandLine call is important here, from Microsoft.Extensions.Configuration.CommandLine" package).

var config = new ConfigurationBuilder()
    .AddCommandLine(args)
    .Build();

var host = new WebHostBuilder()
    .UseConfiguration(config)
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .Build();

host.Run();

Then run it via dotnet run server.urls=http://0.0.0.0:4000.

When you run IIS/IISExpress the kestrel port will be determined by UseIISIntegration().

Tseng
  • 61,549
  • 15
  • 193
  • 205
  • Do you mean **should** or **shouldn't** change the *launcherSettings.json* file manually? I suspect that you mean **not** to change it and that it's a typo in your answer. Please advise. – Konrad Viltersten Nov 11 '16 at 23:01
  • @KonradViltersten: Yes, sorry. Fixed it – Tseng Nov 12 '16 at 00:39
  • just to help others, it might as well be better to add the `using` required, in my case I had to include the `using Microsoft.Extensions.Configuration;` for the `ConfigurationBuilder` to work. In VS it's easy, it will just tell you that you're missing it, but in VSCode I don't see that help provided (unless my C# plugin isn't configured correctly) – ghiscoding May 23 '17 at 20:45
  • I could not run it from the command line via: dotnet run server.urls=http://0.0.0.0:4000. Had to write: dotnet run --urls=http://0.0.0.0:4000 – Stefan Ahlm Oct 11 '18 at 08:00
3

Since .NET Core 2.0 you don't have to maintain hosting.json or modify app startup anymore. There is built-in support for setting app port, explained here: https://stackoverflow.com/a/49000939/606007

Random
  • 4,519
  • 2
  • 38
  • 46