Multi-lingual websites with ASP.NET MVC

后端 未结 2 423
一整个雨季
一整个雨季 2020-12-03 02:19

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 engin

相关标签:
2条回答
  • 2020-12-03 02:28

    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" })%>
    
    0 讨论(0)
  • 2020-12-03 02:31

    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.

    0 讨论(0)
提交回复
热议问题