How to change port number in ASP.NET Core?


I have started coding in ASP.NET Core, so I am not sure what is the proper way to change port number of website? Is it using appsettings.Json file or we need to update startup.cs? Thanks.


Asked by:- Vinnu
1
: 2473 At:- 11/10/2022 3:30:09 PM
ASP.NET Core change default port number







1 Answers
profileImage Answered by:- vikas_jk

In Visual Studio 2017 or Visual Studio 2019/2022, you can easily update .NET Core Application port number by navigating to Project -> Properties -> launchSettings.json file

In this file, you will need to update JSON for iisSettings -> applicationUrl and profiles -> ProjectName -> applicationUrl

So it would look like below

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:5115", // Update this for changing port number
      "sslPort": 44338
    }
  },
  "profiles": {
    "EFCoreStoredProcedure": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5115", //Update this also for changing port number
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

If you are using Kestrel, then you can do it using appsettings.json

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

Using C#

You can also do change port using Program.cs in .NET Core 6 or Web API

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run("http://localhost:5511");

Using Command Line

dotnet run --urls http://localhost:5511

That should work.

1
At:- 11/10/2022 3:49:23 PM
Thanks for adding answer, I tried IISsettings to change port and it works for me in .NET Core 6. 0
By : Vinnu - at :- 11/10/2022 3:51:48 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use