Paulo Morgado

.NET Development & Architecture

Recent Articles

view all

Events

Projects

Recent Readers

Visitor Locations

Visitor Locations

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

Web Site Globalization With ASP.NET Routing

For those who don’t know, I have this web site http://PauloMorgado.NET/ that I use both as a web presence besides my blogs and a playfield.

Because I write both in English and Portuguese, I wanted the web site to have both English and Portuguese versions. This is easily accomplished by using ASP.NET Globalization and Localization.

But I wanted to do more than guessing the user’s language form her/his web browser languages. I wanted something like the MSDN and TechNet web sites have where the culture is embedded in the URL which makes it easy for the user to choose in which language she/he wants to see the web site.

With the release of the ASP.NET Routing, this is as easy as writing a custom route handler that sets the culture for the request and returns the requested page handler.

Something like this:

public class GlobalizationRouteHandler : global::System.Web.Routing.IRouteHandler
{
    System.Globalization.CultureInfo culture;
    System.Globalization.CultureInfo uiCulture;

    public GlobalizationRouteHandler(System.Globalization.CultureInfo culture)
        : this(culture, culture)
    {
    }

    public GlobalizationRouteHandler(CultureInfo culture, CultureInfo uiCulture)
    {
        if (culture == null)
        {
            throw new ArgumentNullException("cultureInfo", "cultureInfo is null.");
        }

        if (uiCulture == null)
        {
            throw new ArgumentNullException("uiCulture", "uiCulture is null.");
        }

        this.culture = culture;
        this.uiCulture = uiCulture;
    }

    private GlobalizationRouteHandler()
    {
    }

    #region IRouteHandler Members

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        Thread.CurrentThread.CurrentCulture = this.culture;
        Thread.CurrentThread.CurrentUICulture = this.uiCulture;

        string path = "~/" + (requestContext.RouteData.Values["path"] as string);

        var physicalPath = requestContext.HttpContext.Server.MapPath(path);
        if (System.IO.Directory.Exists(physicalPath))
        {
            path = VirtualPathUtility.Combine(path, "Default.aspx");
        }

        var httpHandler = BuildManager.CreateInstanceFromVirtualPath(path, typeof(IHttpHandler)) as IHttpHandler;

        return httpHandler;
    }

    #endregion
}

And now it’s only a matter of registering the handled cultures:

routes.Add("en", new Route("en/{*path}", new GlobalizationRouteHandler(CultureInfo.GetCultureInfo("en-US"))));
routes.Add("pt", new Route("pt/{*path}", new GlobalizationRouteHandler(CultureInfo.GetCultureInfo("pt-PT"))));

Comments

Mahdi Taghizadeh said:

Hi!

Thank you for your solution but I faced an issue; using the following code in my Global.asax I receive a 404 error page for /en/admin URL:

public static void RegisterRoutes(RouteCollection routes)

       {

           routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

           routes.Add("en", new Route("en/{*path}", new GlobalizationRouteHandler(CultureInfo.GetCultureInfo("en-US"))));

           routes.Add("fa", new Route("fa/{*path}", new GlobalizationRouteHandler(CultureInfo.GetCultureInfo("fa-IR"))));

           routes.MapRoute(

               "AdminHome",

               "{language}/admin",

               new { controller = "Admin", action = "Index" }

           );

       }

# February 8, 2010 8:51 AM

Paulo Morgado said:

What web server are you running this on?

If you change eh route to "{language}/admin.aspx", does it run?

# February 8, 2010 7:28 PM

Mahdi Taghizadeh said:

No! That's not the problem; I'm using IIS 7.

# February 9, 2010 1:19 AM

Paulo Morgado said:

I'm also using IIS7 in integrated pipeline mode:

<configuration>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </modules>
</configuration>

And you?

# February 9, 2010 7:02 PM

Mahdi Taghizadeh said:

Same.

# February 10, 2010 7:41 AM

Paulo Morgado said:

Oh! I think you need to add the AdminHome route in the first place. Otherwise the en route will be chosen.

# February 10, 2010 6:56 PM

Marco said:

Hi i have problem to configurate your code, you can help me to see all the step for installating correctly routing?

Thanls

# June 1, 2010 3:50 AM

Paulo Morgado said:

Marco,

Can you elaborate a bit more on the problems you are having?

# June 6, 2010 8:25 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)