Just a simple redirect ASPX page
Published Aug 23 2019 08:45 AM 64.5K Views
Microsoft

You need to set a very simple redirect, from a specific location on your IIS web site or application, to a new URL? Then place this very simple default.aspx page at that location; in most of the cases, it works like a charm.

 

<%@ Page Language="C#"%>
<%
    Response.RedirectLocation = "http://new.location.net/optional/path";
    Response.StatusCode = 301;   //   301 = Moved Permanently   |   302 = Object moved   |   307 = Temporary redirect
%>

 

This is aimed at those of us too lazy to enable and use the HTTP Redirection feature of IIS. That would work with much better performance, and would cover some additional functionality, and doesn't need the .NET managed pipeline... But sometimes lazy is good :)

For those in need of complex redirects, the optional UrlRewrite module is THE solution, but you'll need to install and learn creating redirect rules.

Best of luck to all!

3 Comments
Copper Contributor

I'd use a generic handler (ASHX):

 

<%@ WebHandler Language="C#" Class="Handler" > 
  
using System;  
using System.Web;  
  
public class Handler : IHttpHandler  
{  
    public void ProcessRequest(HttpContext context)  
    {  
        context.Response.RedirectLocation = "<a href="http://new.location.net/optional/path" target="_blank">http://new.location.net/optional/path</a>";  
        context.Response.StatusCode = 301; // 301 = Moved Permanently | 302 = Object moved | 307 = Temporary redirect  
    }  
  
    public bool IsReusable => false;  
}

 

No need to load session state or go through the whole page life cycle.

 

Or use Application_BeginRequest on global.asax:

 

 

protected void Application_BeginRequest(object sender, EventArgs e)
{
    var context= ((System.Web.HttpApplication) sender).Context;

    if (/* whatever condition */)
    {
        context.Response.RedirectLocation = "<a href="http://new.location.net/optional/path" target="_blank">http://new.location.net/optional/path</a>";  
        context.Response.StatusCode = 301; // 301 = Moved Permanently | 302 = Object moved | 307 = Temporary redirect
        context.Response.End();
    }
}

 

 

 

 

Microsoft

True, Default.aspx is not the most performant solution. But it is very likely that a "default.aspx" entry exists in the Default Documents configuration. Your solution with Global.asax looks nice; I'm not sure if you can put a global.asax on any folder level in an app.

Copper Contributor

You can only have a global.asax at the root of the web application. That's what /* whatever condition */ is for.

 

You didn't mention the requirement for being on of the DefaultDocuments. But you can add Default Documents (https://docs.microsoft.com/en-us/iis/configuration/system.webserver/defaultdocument/) and you can also set a specific URL or extensions to a specific handler or handler factory (https://docs.microsoft.com/en-us/iis/configuration/system.webserver/handlers/add).

Version history
Last update:
‎Aug 23 2019 08:45 AM
Updated by: