88

I added the following section in project.json.

  "commands": {
    "run": "run server.urls=http://localhost:8082",
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:8082",
    "weblistener": "Microsoft.AspNet.Hosting --server WebListener --server.urls http://localhost:8082"
  },

However, it still shows "Now listening on: http://localhost:5000" when run it using dotnet myapp.dll?

BTW, will clients from other machine be able to access the service?

abatishchev
  • 95,331
  • 80
  • 293
  • 426
ca9163d9
  • 24,345
  • 44
  • 175
  • 352

18 Answers18

65

Yes this will be accesible from other machines if you bind on any external IP address. For example binding to http://*:80 . Note that binding to http://localhost:80 will only bind on 127.0.0.1 interface and therefore will not be accesible from other machines.

Visual Studio is overriding your port. You can change VS port editing this file Properties\launchSettings.json or else set it by code:

        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseUrls("http://localhost:80") // <-----
            .Build();

        host.Run();

A step by step guide using an external config file is available here.

Sridhar Ratnakumar
  • 75,681
  • 63
  • 142
  • 179
Gerardo Grignoli
  • 12,593
  • 6
  • 52
  • 61
  • 1
    In addition, one can check the Microsoft's reference on overriding configuration at [Overriding configuration](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting?view=aspnetcore-2.1&tabs=aspnetcore2x#overriding-configuration) – Ivan Maia May 09 '18 at 20:50
  • 1
    This statement helped me: **You can change VS port by editing** `Properties\launchSettings.json` You can define a new launch profile, add a Firewall exception, then have the front end developer use your live server as the two of you make changes together. – Zachary Scott Mar 07 '22 at 19:24
47

It's working to me.

I use Asp.net core 2.2 (this way supported in asp.net core 2.1 and upper version).

add Kestrel section in appsettings.json file. like this:

{
  "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://localhost:4300"
      }
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

and in Startup.cs:

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
      var builder = new ConfigurationBuilder()
         .SetBasePath(env.ContentRootPath)
         .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
         .AddEnvironmentVariables();

      Configuration = builder.Build();
}
AminRostami
  • 2,196
  • 3
  • 19
  • 41
  • 2
    And `"Url": "http://*:80"` for public facing (don't use for production but useful to for example demo something to a client) – Ruslan Mar 25 '20 at 04:16
  • Error CS0104 'IHostingEnvironment' is an ambiguous reference between 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' and 'Microsoft.Extensions.Hosting.IHostingEnvironment' – MindRoasterMir Mar 12 '21 at 20:06
  • @MindRoasterMir; `IHostingEnvironment` is referenced from `Microsoft.AspNetCore.Hosting`. – AminRostami Mar 13 '21 at 04:56
31

Use simply dotnet YouApp.dll --urls http://0.0.0.0:80.

P.S. I don't know why I need to google this everytime and everytime it doesn't show up. So here it is.

  • 2
    You're not alone. Because all developers have the question: How can change WebAPI port But Microsoft said: Add the urls param – Võ Quang Hòa Jan 17 '22 at 03:49
25

In Asp.net core 2.0 WebApp, if you are using visual studio search LaunchSettings.json. I am adding my LaunchSettings.json, you can change port no as u can see.

enter image description here

GirishBabuC
  • 1,091
  • 13
  • 19
  • 3
    Girish Babu when i am updating the port number and running the application it throwing me the error in popup saying "Unable to connect to web server "IIS Express" , how to resolve this? – Rohit Sharma Aug 03 '19 at 13:58
  • @RohitSharma, another process on your computer may be using the updated port number. Try a different number. – Theophilus Feb 11 '22 at 15:35
16

In visual studio 2017 we can change the port number from LaunchSetting.json

In Properties-> LaunchSettings.json.

Open LaunchSettings.json and change the Port Number.

Launch

Change the port Number in json file

enter image description here

Sathish
  • 1,911
  • 12
  • 13
15

Use following one line of code .UseUrls("http://*:80") in Program.cs

Thus changing .UseStartup<Startup>()

to

.UseStartup<Startup>() .UseUrls("http://*:80")

Akshay Vijay Jain
  • 9,370
  • 6
  • 44
  • 50
8

Go to your program.cs file add UseUrs method to set your url, make sure you don't use a reserved url or port

 public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()

                // params string[] urls
                .UseUrls(urls: "http://localhost:10000")

                .Build();
    }
Eyayu Tefera
  • 623
  • 7
  • 7
8

3 files have to changed appsettings.json (see the last section - kestrel ), launchsettings.json - applicationurl commented out, and a 2 lines change in Startup.cs

Add below code in appsettings.json file and port to any as you wish.

  },

 "Kestrel": {

   "Endpoints": {

     "Http": {

       "Url": "http://localhost:5003"

     }

   }

 }

 }

Modify Startup.cs with below lines.

using Microsoft.AspNetCore.Server.Kestrel.Core;

services.Configure<KestrelServerOptions>(Configuration.GetSection("Kestrel"));

Aditya Y
  • 331
  • 3
  • 4
6

We can use this command to run our host project via Windows Powershell without IIS and visual studio on a separate port. Default of krestel web server is 5001

$env:ASPNETCORE_URLS="http://localhost:22742" ; dotnet run
Abdus Salam Azad
  • 4,110
  • 40
  • 28
6

All the other answer accounts only for http URLs. If the URL is https, then do as follows,

  1. Open launchsettings.json under Properties of the API project.

    enter image description here

  2. Change the sslPort under iisSettings -> iisExpress

A sample launchsettings.json will look as follows

{
  "iisSettings": {
    "iisExpress": {
      "applicationUrl": "http://localhost:12345",
      "sslPort": 98765 <== Change_This
    }
  },
Kolappan N
  • 2,875
  • 2
  • 31
  • 38
6

will clients from other machine be able to access the service?

add to appsettings.json

  "Urls": "http://0.0.0.0:8082",
Shaybakov
  • 500
  • 5
  • 7
2

in your hosting.json replace"Url": "http://localhost:80" by"Url": "http://*:80" and you will be able now access to your application by http://your_local_machine_ip:80 for example http://192.168.1.4:80

Amara Miloudi
  • 104
  • 1
  • 6
2

in appsetting.json

{ "DebugMode": false, "Urls": "http://localhost:8082" }

mostafa kazemi
  • 257
  • 3
  • 6
1

you can also code like this

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

and set up your application by command line :dotnet run --server.urls http://*:5555

liam
  • 29
  • 4
1

Maybe it's because I am not using Core yet. My project didn't have a LaunchSettings.json file but that did prompt me to look in the project properties. I found it under the Web tab and simply changed the project url: enter image description here

Chris Catignani
  • 4,580
  • 11
  • 39
  • 46
Anthony Griggs
  • 1,339
  • 2
  • 15
  • 36
1

I had a similar issue with a Kubernetes deployment after upgrading to .NET 6. The solution was simply to add the following environment variable to the deployment:

- name: Kestrel__Endpoints__Http__Url
  value: http://0.0.0.0:80

This will work anywhere else where you can use an environment variable

Craig Miller
  • 538
  • 9
  • 8
0

If you want to run on a specific port 60535 while developing locally but want to run app on port 80 in stage/prod environment servers, this does it.

Add to environmentVariables section in launchSettings.json

"ASPNETCORE_DEVELOPER_OVERRIDES": "Developer-Overrides",

and then modify Program.cs to

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseKestrel(options =>
                    {
                        var devOverride = Environment.GetEnvironmentVariable("ASPNETCORE_DEVELOPER_OVERRIDES");
                        if (!string.IsNullOrWhiteSpace(devOverride))
                        {
                            options.ListenLocalhost(60535);
                        }
                        else
                        {
                            options.ListenAnyIP(80);
                        }
                    })
                    .UseStartup<Startup>()
                    .UseNLog();                   
                });
HirenP
  • 41
  • 1
  • 6
0

I created my project using Visual Studio 2022, so in Project/Properties/launchSettings.json there are two parts for this topic: 1- for lunching in IISExpress :

"iisSettings": {
..
"iisExpress": {
  "applicationUrl": "http://localhost:31520",
  "sslPort": 44346
}

},

2- for lunching through IDE:

"profiles": {
"MaxTemplate.API": {
  ...
  "applicationUrl": "https://localhost:7141;http://localhost:5141",
  ...
  }
},

For example you can change the port 7141 to 5050 and run the project again.

Max
  • 19
  • 2