November Happy Hour will be moved to Thursday December 5th.

Peter Lapalus
Apr 20, 2009
  12948
(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
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog