Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
742 views
in Technique[技术] by (71.8m points)

url rewriting - Multi-lingual websites with ASP.NET MVC

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" })%>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...