Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Peter Lapalus
Apr 20, 2009
  13016
(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
How to: create Decimal metafield with custom precision

If you are using catalog system, the way of creating metafields are easy – in fact, you can forget about “metafields”, all you should be using is t...

Quan Mai | Jan 16, 2025 | Syndicated blog

Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

Luc Gosso (MVP) | Jan 14, 2025 | Syndicated blog

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

Per Nergård | Jan 14, 2025

Product Recommendations - Common Pitfalls

With the added freedom and flexibility that the release of the self-service widgets feature for Product Recommendations provides you as...

Dylan Walker | Jan 14, 2025

My blog is now running using Optimizely CMS!

It's official! You are currently reading this post on my shiny new Optimizely CMS website.  In the past weeks, I have been quite busy crunching eve...

David Drouin-Prince | Jan 12, 2025 | Syndicated blog