104

I've deployed my c#, asp.net 5, mvc 6 app to a windows 2008 server. I've fired up dnx web and it is listening to port 5000 and works fine when accessing from local computer.

How do I get it to listen to non-localhost requests?

P.S. This question is not a duplicate of this...it refers to asp.net pre RC1 when hosting.ini actually had an .ini format. Now, it's JSON and I can't find any documentation on what should actually be in it.

P.P.S. The real solution is in the non-accepted answer to the linked question, with a massive caveat. Steps:

  1. Change your project.json per the linked answer.
  2. Publish your project to your server.
  3. On the server, go to ...\approot\src\YourProject folder and open a command window there.
  4. Run dnx web - it will fail
  5. Run dnu restore
  6. Run 'dnu build`
  7. Run 'dnx web` - the web server should now start fine

P.S. For people upvoting this question. It's outdated. Very badly outdated!

It applied to the early versions of the .NET Core. The question and the answers certainly aren't applicable for the current versions of the framework (e.g. 2.x, 3.x)

Community
  • 1
  • 1
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • Possible duplicate of [ASP.NET 5 Kestrel connect within LAN](http://stackoverflow.com/questions/33975949/asp-net-5-kestrel-connect-within-lan) – chue x Dec 11 '15 at 00:04
  • Well the second answer in the linked question refers to the JSON format. It's not useful for you either? – chue x Dec 11 '15 at 02:52
  • @chuex I tried that, and initially it crashed the web server on startup. Not sure why. Then I did `dnu restore`, followed by `dnu build` and then `dnx web` properly started the web server. I don't know why its happening, but at least I am able to make it work. There is definitely a bug that needs to be addressed before the final release. For now, this is the workaround. – AngryHacker Dec 11 '15 at 07:06
  • 1
    I edited by question to reflect the solution. – AngryHacker Dec 11 '15 at 07:07
  • @AngryHacker: You can specify `server.urls` parameter in application configuration file or command line parameters. The value can have multiple semicolon separated values see [here](https://github.com/aspnet/KestrelHttpServer/blob/dev/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs#L65). The file name of configuration can be [hosting.json](https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/WebApplication.cs#L14), but it can be not only json: see [the issue](https://github.com/aspnet/Hosting/issues/269). `--config` can be used to specify the config file name – Oleg Dec 11 '15 at 08:32
  • 1
    I know this question had already been answered and marked as solved, but still I found a great post by Rick Strahl on his web log called [External Network Access to Kestrel and IIS Express in ASP.NET Core](https://weblog.west-wind.com/posts/2016/Sep/28/External-Network-Access-to-Kestrel-and-IIS-Express-in-ASPNET-Core) so I wanted to share it with everyone who encounter this issue. – Liran Friedman Jun 28 '17 at 06:36
  • I guess we're supposed to downvote this question if we're using a modern version of .Net? Kind of confusing. – Andrew Koster Mar 18 '20 at 17:29
  • 3
    I read the big letters on being outdated but I absolutely needed the replies here even for a web API in .NET Core 3.1. I had first tried to bind to 0.0.0.0:5000 but IPv6 apparently happens around here and so this didn't work. Meanwhile [::]:5000 didn't work either for external IPv4 access. I had no idea binding to *:5000 was even a thing as we're now venturing out of the IPv4/IPv6 standard. So very helpful! – Jonas Nov 27 '20 at 10:25

7 Answers7

126

The default configuration file used by Kestrel server is hosting.json. The name was changed multiple times in different beta versions. If you use now project.json with the following "command" section

"commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
}

then during starting the server from the command line by

dnx web

the file hosting.json will be read. The file

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

will configure the server to listen 5000 on every IP4 address. The configuration

{
    "server.urls": "http://::5000;http://0.0.0.0:5000"
}

will inform to listen 5000 on both IP4 and IP6 address.

One can specify alternative configuration files by usage ASPNET_ENV environment variable or by the usage of --config myconfig1.json (or config=myconfig1.json). For example you can use

SET ASPNET_ENV=Development

and to create hosting.Development.json file with specific configuration. Alternatively you can use project.json with

"commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
    "webProd": "Microsoft.AspNet.Server.Kestrel --config prod.json"
}

and start the server by usage

dnx webProd

I have to remind additionally that it could be required that you allow to additionally listen and to register (to start dnx web). It's required because of the firewall and the local security of listening new TCP/HTTP ports. Something like below should make local registering and listening of 5000 port for everybody (IPv4 and IPv6):

netsh http add iplisten ipaddress=0.0.0.0:5000
netsh http add iplisten ipaddress=::5000
netsh http add urlacl url=http://+:5000/ user=\Everyone

To be more secure you can adjust the above configuration to grant minimal rights.

UPDATED: Thanks @BlaneBunderson. One can use * instead of IP address (like http://*:5000) to listen on any IP4 and IP6 addresses from any interface. One should be carefully and not use these

  • http://*:5000;http://::5000
  • http://::5000;http://*:5000
  • http://*:5000;http://0.0.0.0:5000
  • http://*:5000;http://0.0.0.0:5000

because it will require to register IP6 address :: or IP4 address 0.0.0.0 twice.

Corresponds to the announcement

Technically, any hostname that isn't "localhost" or a valid IPv4 or IPv6 address will cause Kestrel to bind to all network interfaces.

I think that the behavior could be changed in the future. Thus I would recommend to use only *:5000, 0.0.0.0:5000 and ::5000 form for registering of any IT address.

UPDATED 2: ASP.NET Core RC2 changes (see the announcement) the behavior of loading the defaults. One have to make changes in the Main to load the settings from hosting.json and the command line parameters. Below is an example of the usage

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("hosting.json", optional: true)
        .AddEnvironmentVariables(prefix: "ASPNETCORE_")
        .AddCommandLine(args)
        .Build();

    var host = new WebHostBuilder()
        .UseUrls("http://*:1000", "https://*:1234", "http://0.0.0.0:5000")
        .UseEnvironment("Development")
        .UseConfiguration(config)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

The above code use three bindings: "http://*:1000", "https://*:1234", "http://0.0.0.0:5000" by default instead of usage the default port 5000 by default (to be exact the usage of http://localhost:5000). The call of .UseConfiguration(config) are made after .UseUrls. Thus the configuration loaded from hosting.json or the command line overwrite the default options. If one remove .SetBasePath(Directory.GetCurrentDirectory()) line then the hosting.json will be loaded from the same directory where the application dll will be compiled (for example bin\Debug\netcoreapp1.0).

One can use execution like

dotnet.exe run --server.urls=http://0.0.0.0:5000

to overwrite the default settings (from UseUrls) and the settings from "server.urls" property of hosting.json if it's exist.

In the same way one could overwrite the ULR settings by setting the environment variable

set ASPNETCORE_SERVER.URLS=http://localhost:12541/

then the default start of the application using dotnet.exe run will use http://localhost:12541/ for binding.

You can find here an example of the usage of HTTPS binding.

REMARK: The name of environment variable is changed from ASPNETCORE_SERVER.URLS to ASPNETCORE_URLS in later versions of ASP.NET(see here the documentation of ASP.NET Core 3.1).

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • 1
    In addition you can use "server.urls": "http://*:5000" instead of "server.urls": "http://0.0.0.0:5000" (Personally I think the * makes a little more sense.) – Blane Bunderson Dec 13 '15 at 21:54
  • @BlaneBunderson: Thank for your suggestion! I have to add that `http://*:5000` is the same as `http://::5000;http://0.0.0.0:5000` and not `http://0.0.0.0:5000`. It force listening for both IPv4 and IPv6. You can verify it by usage `http://*:5000;http://::5000` or `http://::5000;http://*:5000`. One get the error "Microsoft.AspNet.Server.Kestrel.Networking.UvException: Error -4091 EADDRINUSE address already in use" during registering the second address. In some scenarios registering of both IPv4 and IPv6 is good, in another scenarios it's not good. In any way it's good to mention the way. – Oleg Dec 15 '15 at 00:04
  • Excellent update for RC2, thanks for the detailed explanation – Trygve May 22 '16 at 12:33
  • wouldn't `.UseStartup()` override `.UseConfiguration(config)` in your "update 2" example? – nicks Dec 01 '16 at 17:58
  • there is a typo in that environment variable, it should be ASPNETCORE_URLS – nrjohnstone Mar 18 '20 at 08:38
  • @nrjohnstone: To be exactly: it's not a typo. The answer is written **before** ASP.NET Core 1.0 was released. The name of environment variable is changed during the versions. It was `ASPNETCORE_SERVER.URLS` initially and it was changed later (see [here](https://github.com/aspnet/Hosting/blob/master/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs#L159-L162) the code fragment of ASP.NET Core 2.2). Now (in version ASP.NET Core 3.1) it's `ASPNETCORE_URLS` (see [here](https://learn.microsoft.com/en-US/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1#endpoint-configuration)). – Oleg Mar 18 '20 at 09:12
  • this worked for me - > set ASPNETCORE_SERVER.URLS=http://0.0.0.0:5000/ – Say Jul 28 '20 at 09:57
  • What does the + in URLS mean? – Arrow_Raider Jun 02 '21 at 16:12
33

In RC2 the commands section of project.json is no longer used. I haven't gotten Kestrel to pick up the hosting.json yet, but you can programatically set the port in the Main of the application where the new WebHostBuilder is created and configured. Just add .UseUrls() method like in the sample below

    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseUrls("http://0.0.0.0:5000/")
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
Trygve
  • 2,445
  • 1
  • 19
  • 23
  • 3
    This was a big help. I used `.UseUrls("http://*:5000")` instead, and port forwarded 5000 with empty Host IP on VirtualBox, and finally I can get to the Docker-hosted .NET Core endpoints from my Mac. Sweet! – Mark Larter May 25 '16 at 02:24
  • For info on using hosting.json in RC2 and more detailed explanation, read Oleg's answer above, especially "Updated 2" – Trygve May 25 '16 at 07:56
  • 2
    Also applicable to 1.0.0-preview2-003121, docker image – linquize Jul 01 '16 at 06:08
26

If you use asp.net core 2.1+,modify config section in appsettings.json.

"Kestrel": {
  "EndPoints": {
    "Http": {
      "Url": "http://0.0.0.0:5002"
    }
  }
},
Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66
menxin
  • 2,044
  • 1
  • 17
  • 15
19

For AspNetCore 3.1+ just add the following line in the file appsettings.json:

"Urls": "http://*:80"
Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63
12

If you are trying to put an ASP.NET Core application inside a docker container (which was my use case for needing to listen to non-localhost addresses), note that this use case has already been put together for you by Microsoft. You can see the full glory at https://hub.docker.com/r/microsoft/aspnetcore/

At current (v1.0.1) the key magic for solving this problem is that the source Dockerfile contains a url environment variable setting, and the application does not attempt to override this. (Indeed, a containerized application should internally assert as little as possible about the environment where it will run.)

ENV ASPNETCORE_URLS http://+:80

Note the plus sign rather than asterisk there. I actually recommend visiting the above dockerhub link over reading my answer for as long as the link is good. Version 1.1 is just around the corner, and things may change again in the future.

When running the container, make sure to expose guest port 80, per the environment variable setting. For example:

docker run -d -p 8000:80 myapp
curl localhost:8000
GrandOpener
  • 1,943
  • 1
  • 17
  • 25
7

Another option is to modify launchSettings.json. Replace "applicationUrl": "http://localhost:5000", with "applicationUrl": "http://0.0.0.0:5000",. Like so:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:3031"
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication1": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://0.0.0.0:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
ubershmekel
  • 11,864
  • 10
  • 72
  • 89
1

Set the environment variable ASPNETCORE_URLS to http://0.0.0.0:5000/.

If running from Visual Studio, you add the environment variable from the Debug tab of the project properties.

Edward Brey
  • 40,302
  • 20
  • 199
  • 253