使用 ASP.NET MVC 的多语言网站 [英] Multi-lingual websites with ASP.NET MVC

查看:32
本文介绍了使用 ASP.NET MVC 的多语言网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建多语言网站(使用 ASP.NET 网络表单)时,我将使用 HTTP 模块重写 URL 以最终得到一些友好的内容(对于人类和搜索引擎),例如:

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

我(新手)对MVC的理解是URL应该采用

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

控制器/动作/标识符

因此,使用 MVC 复制上述功能最终会得到类似于以下内容的 URL:

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

问题..

  • 我可以在 URL 的控制器"段之前插入国家/地区代码吗?
  • 是否可以将products"和productos"映射到同一个控制器?

感谢您的帮助

除了下面 Panos 的回答,我在 ASP.NET 网站上找到了更多信息.

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

推荐答案

URL 几乎可以采用您喜欢的任何其他形式.有关详细信息,请查看 ASP.NET MVC 框架(第 2 部分):URL 路由.刚开始(因为我不确定这是否是最佳解决方案),您可以在 global.asax 中添加两条新路线:

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 = "" }
        );

这些路由理解以下 URL(并将它们都映射到 ProductsControllerActionResult Category(string id, string 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

如果你想在你的视图中创建这样的 URL,你可以使用类似的东西:

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

这篇关于使用 ASP.NET MVC 的多语言网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆