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.
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": "*"
}
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");
dotnet run --urls http://localhost:5511
That should work.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly