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": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }

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

Answers:

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)
{
    app.UseHttpsRedirection();
}

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 stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x