Peter Lapalus
Apr 20, 2009
  12815
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024