Peter Lapalus
Apr 20, 2009
  12871
(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
Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024

How to write a bespoke notification management system

Websites can be the perfect vehicle for notifying customers of important information quickly, whether it’s the latest offer, an operational message...

Nicole Drath | Sep 25, 2024

Optimizely DAM – An Introduction

I presented a talk about the Optimizely DAM at the OMVP summit during Opticon 2024 in Sweden. I have now converted that talk into two blog posts....

Andrew Markham | Sep 25, 2024 | Syndicated blog

Simple and Effective Personalization with Optimizely Data Platform (ODP)

As we dive into the amazing capabilities of Optimizely One, let’s shine a spotlight on the Optimizely Data Platform (ODP). This simple tool unifies...

Alex Harris - Perficient | Sep 24, 2024 | Syndicated blog