HttpToHTTPs

Hello Readers,

While developing web application many of us must have observed that after deploying to web server, web page url starts with http but not the secured one https.

So if we want to redirect url from HTTP to HTTPS there is a very simple solution in C#.

Just copy and paste the below code in your “Global.asax.cs” file and you are all done.

protected void Application_BeginRequest()
{
if (!Context.Request.IsSecureConnection)
Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}

 

Note: it will work only in the deployed code that is in your web server but not in localhost.

So while running application in your local machine just comment the above code.

——————————————————————————————————————————–

For now signing off until next post. If you have any queries/suggestions please let me know in the comment section below.

Thanks for reading!

Recommended Articles:

  1. What’s new in .NET Framework 4.7.1 (Part 1)
  2. What’s new in .NET Framework 4.7.1 (Part 2)
  3. What’s new in .NET Framework 4.7.1 (Part 3)
  4. How to convert local path to UNC (Universal) File Path in C#