Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Peter Lapalus
Apr 20, 2009
  12717
(0 votes)

Using redirects in HttpModules with EPiServer

A couple of weeks ago when developing a HttpModule to avoid duplicate content using EPiServer and multi languages, I discovered that EPiServer has implemented an URL-redirect feature in the FriendlyUrlModuleBase class. This feature was added to the EPiServer CMS R2 release.

EPiServer has added an eventhandler on the EndRequest event for the HttpRequest. The eventhandler will always be triggered in the end of the request.

The eventhandler looks like this:

private void EndRequestEventHandler(object sender, EventArgs e)
{
    HttpApplication application = (HttpApplication) sender;
    HttpResponse response = application.Response;
    if (((response.StatusCode == 0x12d) || (response.StatusCode == 0x12e)) || ((response.StatusCode == 0x12f) || (response.StatusCode == 0x133)))
    {
        response.RedirectLocation = this.HttpUrlRewriteToExternal(response.RedirectLocation, true, UrlBuilder.RebaseKind.ToRootRelative);
    }
}

As you can see, EPiServer is using the RedirectLocation property of the response object in the call of the this.HttpUrlRewriteToExternal method if the response.StatusCode is equal to 301 (the HTTP response code for permanent redirect). In the case of this property having a Null value you will get an Exception on this call.

When developing your own HttpModule and you want to make an URL-redirect in that HttpModule, and you are using the Response.Redirect(url) method, the RedirectLocation property will not automatically be set but the StatusCode property will be set to 301 and there by an exception will be thrown.

To avoid this, instead of using Response.Redirect all you need to do is to set the Response.StatusCode to 301 and then set the Response.RedirectLocation property to the new URL.
The redirection will there by be taken cared of by EPiServer on the EndRequest event.

See this example:

class RedirectModule : System.Web.IHttpModule
{
    public void Dispose()
    {
    }
 
    public void Init(System.Web.HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }
 
    private void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
 
        if (sender != null)
        {
            string redirectUrl = "http://www.cloudnine.se";
 
            if (redirectUrl != null)
            {
                app.Response.StatusCode = 301;
                app.Response.RedirectLocation = redirectUrl;
            }
        }
    }
}

An alternative if you don’t want the wait to the end of the request is to first set the RedirectLocation property and then call Response.Redirect(url).

See this example:

class RedirectModule : System.Web.IHttpModule
{
    public void Dispose()
    {
    }
 
    public void Init(System.Web.HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }
 
    private void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
 
        if (sender != null)
        {
            string redirectUrl = "http://www.cloudnine.se";
 
            if (redirectUrl != null)
            {
                app.Response.RedirectLocation = redirectUrl; 
                app.Response.Redirect(redirectUrl);
            }
        }
    }
}
Apr 20, 2009

Comments

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog