JumpStart # 40 – Change default 5000 port of Dotnet Run command

Dotnet Run command by default host the ASP.Net Core application at port 5000. As obviously we cannot host more than one application at 5000 port, we can easily change the default port to new one.

NOTE: Updated this tutorial on 2/26/2017 with MSBuild based Dotnet SDK.

I am using following version of Dotnet SDK which is based on MSBuild/CSProj (remember that previous versions of ASP.Net Core are based on Project.json/XProj.).

 image

Open ASP.Net Core application in Visual Studio and add hosting.json file with following content.

{
  "server.urls": "http://localhost:5001"
}

Now open Program.cs, and update it with following code.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace WebApplication1
{
    public class Program
    {
        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();
        }
    }
}

Basically we create one JSON config file with server urls and gave that as input to WebHost configuration. Now execute dotnet run command.

image

If we navigate to http://localhost:5001, we get the site up and running.

image

Happy Coding and Stay Tuned!!!

You may also like...

  • ramiramilu

    In the newer versions, please try with launchsettings.json file.