views:

212

answers:

3

Has anyone had any experienced with making language dependent routes with ASP.NET MVC? What I would like to do is have localized url's to improve SEO. So for example http://mysite.com/products/cars would map to the same controller/action as http://mysite.com/produkter/bilar?

I have tried browsing around a bit, but I could not find anything similar to this. I'm not even convinced it is really such a good idea, but I would imagine that it would help SEO when users are doing searches in their own language. I would imagine this would take some customization of the mvc route engine.

Edit: Mato definitely has the best solution so far, I would like to see a Custom RouteHandler solution though for reference.

+2  A: 

One thing with SEO is, if a search engine finds two identical documents on two different links, it may reduce page rank for one of the pages. You need then to translate the page content as well.

Developer Art
The content will be localized as well of course :)
Runeborg
+1  A: 

You could use regular expression routes (http://iridescence.no/post/Defining-Routes-using-Regular-Expressions-in-ASPNET-MVC.aspx), and then do something like

routes.Add(new RegexRoute(@"^(Products|Produkter)$", new MvcRouteHandler())
{
    Defaults = new RouteValueDictionary(new { controller = "Products"  })
});
Jan Jongboom
Yes, this is interesting. But I would like to have a more automated way of doing it. I should probably look more into extending the Route class similar to RegexRoute.
Runeborg
+3  A: 

You could implement an own controller factory class which translates the controller name before initializing it. You could for example store the translations in a resource file or in a DB. The easiest way to do this is to inherit from DefaultControllerFactory and to overwrite the CreateController function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.Mvc
{
    class CustomControllerFactory : DefaultControllerFactory
    {
        public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            /**
             * here comes your code for translating the controller name 
             **/

            return base.CreateController(requestContext, controllerName);
        }
    }
}

The last step would be to register your controller factory implementation when the application starts (in the Global.asax).

namespace MyApplication
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            ControllerBuilder.Current.SetControllerFactory(typeof(CustomControllerFactory));
        }
    }
}
Mato
This definately sounds interesting, unfortunately there is also other parts to the url than the controller name, such as area and action. So I think it is probably best solved at a route level. But maybe this can be handled here as well somehow?
Runeborg
Since you have access to the Request object through RequestContext you can manipulate every parameter you want to. So there is no problem translating the area or the action at this point.
Mato
Hm, you mean by modifying the the area/controller/action of routedata? Yes this might work actually if this is evaluated after the route.
Runeborg