ASP.NET Core redirect http to https but I don’t want to do it in localhost also I use a different port

In ,net core 2.1, I want to redirect from http to https.

services.AddHttpsRedirection(options =>
    options.RedirectStatusCode = StatusCodes.Status301MovedPermanently;
    options.HttpsPort = 5001;

However in localhost I don’t use https, I just use http. Also I use a different port.
"MyProject": {
  "commandName": "Project",
  "launchBrowser": true,
  "applicationUrl": "http://localhost:50642",
  "environmentVariables": {

It doesn’t work in localhost unless I change the port to 5001 in launchSettings.json. How to make it working?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

if you don’t want to redirect http > https in development, then in your Configure method use an if around app.UseHttpsRedirection();

if (appSettings.HttpsPort.HasValue)

Method 2

Just change the port as

"applicationUrl": "https://localhost:5001",

When you click F5 to debug, Visual Studio 2017 will ask you to install a cert for you automatically. You don’t have to manually to do it.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x