10

When building a multi-lingual website (with ASP.NET web forms), I'll use an HTTP module to rewrite the URLs to end up with something friendly (for humans & search engines) like:

uk/products/product_category_one/sub_category_one/index.aspx
uk/products/product_category_one/sub_category_one/widget_mk5.aspx
es/productos/categoría_de_producto_una/widget_mk5.aspx

My (newbie) understanding of MVC is that the URL should take the format of

Controller / Action / Identifier

so replicating the functionality above with MVC will end up with URLs similar to:

products/category/123/product_category_one/sub_category_one
products/items/456/widget_mk5

Questions..

  • Can I insert a country code into the URL before the 'controller' segment?
  • Is it possible to map 'products' and 'productos' to the same controller?

Thanks for your help

Edit: In addition to Panos' answer below I found more information on the ASP.NET Website.

Mat
  • 202,337
  • 40
  • 393
  • 406
Nick
  • 5,616
  • 10
  • 52
  • 72

2 Answers2

12

The URL can take almost any other form you like. For more info, check ASP.NET MVC Framework (Part 2): URL Routing. Just for starting (since I am not sure if it is the optimum solution), you can add two new routes in your global.asax:

        routes.MapRoute(
            "ukRoute",
            "{lang}/Products/{action}/{id}/{subcategory}",
            new { lang = "uk", controller = "Products", action = "Index", id = "", subcategory = "" }
        );
        routes.MapRoute(
            "esRoute",
            "{lang}/Productos/{action}/{id}/{subcategory}",
            new { lang = "es", controller = "Products", action = "Index", id = "", subcategory = "" }
        );

These routes understand the following URLs (and map both of them to the ActionResult Category(string id, string subcategory) method of ProductsController):

uk/Products/Category/1/A
es/Productos/Category/1/A

If you want to create such URLs in your views you can use something like:

<%= Html.RouteLink("English 1.A", "ukRoute", new { lang = "uk", action = "Category", id = "1", subcategory = "A" })%>
<%= Html.RouteLink("Spanish 1.A", "esRoute", new { lang = "es", action = "Category", id = "1", subcategory = "A" })%>
Panos
  • 18,992
  • 6
  • 45
  • 54
  • does it works? `uk/Products/Category/1/A` `es/Productos/Category/1/A` In both these scenarios it will be directed to the first Route `ukRoute` since lang is a string field and match. – Saanch Apr 17 '13 at 10:15
  • 2
    I think the method introduced [in this blog](http://blog.maartenballiauw.be/post/2010/01/26/Translating-routes-(ASPNET-MVC-and-Webforms).aspx) is a better idea. – VahidNaderi Feb 06 '14 at 09:14
3

You can do this, but keep in mind that not all countries are languages. For example, en-gb is the usual representation for British English, or more specifically, the Great Britain locale for English content, for example. If you can, it's worth following the RFC1766-derived conventions for language-LOCALE.

Search engines actually tend to do a fairly good job dealing with content-negotiation, by the way, so you need not necessarily have separate URIs for the same content in different languages. Google Japan will crawl with ja-JP as the accept language header, for example.

JasonTrue
  • 19,244
  • 4
  • 34
  • 61
  • Regarding search engines you should emit the "lang" and "dir" attributes on the "html" element. I prefer the cookie method which can all be dealt with in the Global.asax code (or even wrapped into a parent HttpApplication base class to fully automate for any MVC or ASPX web site) by saving a "culture" cookie at AquireRequestState (with browser or web site default language) which is easy to change to any preference via a "SetLanguage" application method which just changes the cookie and thread culture. Then the site works without different URLs and the content valid (identifies its language). – Tony Wall Jan 24 '13 at 10:22