DEV Community

Cover image for How to Change Ports in .Net Applications
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

How to Change Ports in .Net Applications

This is a straight forward article of how to change the port of a Dotnet application.

Different ways to change port in a Dotnet application

Using the CLI

dotnet run --urls "http://localhost:5001;https://localhost:5002"
Enter fullscreen mode Exit fullscreen mode

Using the launchSettings.json file

{
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5002;http://localhost:5001"
    },
    "WebApplication1": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5002;http://localhost:5001"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Using the appsettings.json file

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5001"
      },
      "HttpsInlineCertFile": {
        "Url": "https://localhost:5002",
        "Certificate": {
          "Path": "localhost.pfx",
          "Password": "password"
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Using app.Run()


app.Run("http://localhost:5001");
app.Run("https://localhost:5002");
Enter fullscreen mode Exit fullscreen mode

Using the UseUrls() method

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseUrls("http://localhost:5001", "https://localhost:5002")
        .UseStartup<Startup>();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)