A little something about everything

internet marketing and website development made simple..

How to globally redirect all requests from http to https using asp.net

The most effective method will be to use the global.asax file, here is a basic example:

<script language="C#" runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e)	{
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://"))
{
    HttpContext.Current.Response.Status = "301 Moved Permanently"; 
    HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://","https://")); 
}
}
</script>

In some cases you may already have an application using the global.asax file, you can in many cases add the _BeginRequest function or adapt it if it already exists. For example add this after the <script runat="server"> tag but before any other code.

protected void Application_BeginRequest(object sender, EventArgs e)	{
	if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://"))	{
	HttpContext.Current.Response.Status = "301 Moved Permanently"; 
	HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://","https://")); 
	}

In some cases this will throw and error, before sure to check the rest of the global.asax file for any other begin requests that may be conflicting. In some cases you may need to adapt the code to work in parallel with exiting code.