Koen about .Net

January 22, 2011

Culture specific website in ASP.NET MVC

Filed under: Development — Tags: , — koenwillemse @ 00:36

In my personal time I’m working on a web shop application. It’s an application which is really going to be used, so not like the usual throw-away home projects. I could off course just grab an already existing application, but I’m too geeky for that Winking smile. I also wanted to use it to learn a lot about working with ASP.NET MVC and jQuery.

One of the requirements is that it have to support multiple languages. I wanted to do this the way you see it at for instance http://msdn.microsoft.com/nl-nl/ with the culture code in the url. So far so good. I started out by using a feature in ASP.NET MVC which I found, and that was by using a FilterAttribute in combination with an ActionFilter which I found here: http://helios.ca/2009/05/27/aspnet-mvc-and-localization/. Nice solution I thought, so I took that code, modified it a bit to match my scenario and it worked.

But then when I added more views to the application, I started to notice that it was not a good solution at all, because of the following reasons:

  1. I had to change my routes so they would work correctly with the culture code in the url (urls will look like this: http://www.pastechi.nl/nl-NL/Products/Index etc). Maybe this wasn’t necessary and it is because I’m not completely familiar with the routing in ASP.NET, but still, it was not what i wanted.
  2. Every action on my controller now had a parameter cultureCode, which was not used in that method, because the culture code was set in the FilterAttribute code.
  3. It just felt wrong Winking smile

So I started thinking about it and I came to the conclusion that I just brought a knife to a gun fight. The solution works, but is not very usefull. So what would be better? Well, the new solution I’ve got implemented uses url rewriting. The url rewriting is done using the Application_BeginRequest event and takes place before the routing engine of ASP.NET MVC is doing it’s magic. So I created a simple HttpModule which can be used to rewrite the url, by removing the culture code from the url and placing the culture code in a suitable location.

This is the code I wrote (keep in my that this is the POC code, so it should be rewritten a bit to be unit testable etc).

using System;
using System.Text.RegularExpressions;
using System.Web;

namespace HttpModules
    public class CulturePathRewriteModule : IHttpModule
        public void Init(HttpApplication context)
            context.BeginRequest += OnBeginRequest;

        static void OnBeginRequest(object sender, EventArgs e)
            var request = HttpContext.Current.Request;
            var cultureRegEx = new Regex(@"(^\/[A-z]{2}\-[A-z]{2})|(^\/[A-z]{2}\-[A-z]{2}.*)");

            if (cultureRegEx.IsMatch(request.Url.AbsolutePath))
                string cultureCode = request.Url.AbsolutePath.Remove(0, 1).Substring(0, 5);
                string newAbsolutePath = request.Url.AbsolutePath.Remove(0, 6);
                string newUrl = "~" + newAbsolutePath;

                HttpContext.Current.Items["currentCultureCode"] = cultureCode.ToLower();

        public void Dispose()

You probably noticed that I didn’t set the CurrentCulture / CurrentUICulture on the current thread. It’s not that I don’t know that it exists, but I can’t use it. One of the languages that I want to support doesn’t have an official CultureCode. Off course, you can create this if you want, but because the site will be running on a shared hosting partner, that is not an option. The eventual code won’t place it in the context using a hardcoded key, but it will be done using a custom wrapper on the context which I can use typed, but you’ve got the idea.


1 Comment »

  1. I dont understand few issues. But anyway thx for article!

    Comment by pozycjonowanie — February 1, 2011 @ 23:35

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: