[英]Configuring ASP.NET MVC Attribute Routing for multi language site
I'm creating a MVC5 web site that should support multiple languages. 我正在创建一个支持多种语言的MVC5网站。 The structure of the app is complex so I'm using Attribute Routing only. 应用程序的结构很复杂,所以我只使用属性路由。 RouteConfig.cs
is very simple: RouteConfig.cs
非常简单:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
I want to keep language information in URL by adding language identifier after site name. 我希望通过在站点名称后面添加语言标识符来保留URL中的语言信息。 For English, which is default language, URL should remain "clean". 对于默认语言的英语,URL应保持“干净”。 Here are an example: 这是一个例子:
http://test.com/foo/1/bar/2
http://test.com/de/foo/1/bar/2
My first attempt was to use two RoutePrefix
attributes for each controller: 我的第一次尝试是为每个控制器使用两个RoutePrefix
属性:
[RoutePrefix("foo")]
[RoutePrefix("{lang}/foo")]
But MVC doesn't allow to use more than one RoutePrefix
attribute for a controller. 但是MVC不允许为控制器使用多个RoutePrefix
属性。
So now I'm not using RoutePrefix
attributes at all, and specify full route for each controller action: 所以现在我根本不使用RoutePrefix
属性,并为每个控制器操作指定完整路由:
[Route("foo/{a}/bar/{b}")]
[Route("{lang}/foo/{a}/bar/{b}")]
Is there any better way to handle lang
route? 有没有更好的方法来处理lang
路线? Ideally I would like to specify in one place only, not for every controller. 理想情况下,我只想在一个地方指定,而不是每个控制器。
PS. PS。 I'm setting current thread culture by parsing language route in custom filter. 我通过解析自定义过滤器中的语言路径来设置当前的线程文化。
I found the easiest solution (at least for ASP.NET MVC 5.2) is to use a default value for the optional parameter. 我发现最简单的解决方案(至少对于ASP.NET MVC 5.2)是使用可选参数的默认值。
For example, if English is your default language, you can use a single attribute on the Controller: 例如,如果英语是您的默认语言,您可以在Controller上使用单个属性:
[RoutePrefix("{lang=en}/foo")]
or a single attribute on the Action: 或Action上的单个属性:
[Route("{lang=en}/foo/bar")]
Note that optional URI parameters (eg {lang?}
) don't work if they are at the start of the URL. 请注意,如果可选的URI参数(例如{lang?}
)位于URL的开头,则它们不起作用。
If all else fails then I suggest you keep your default routes as is and store the language in a query parameter 如果所有其他方法都失败了,那么我建议您按原样保留默认路由,并将语言存储在查询参数中
For English, your default language, URL will remain "clean" as you intended. 对于英语,您的默认语言URL将按预期保持“干净”。
http://test.com/foo/1/bar/2
But for other languages you include lang
as query parameter 但对于其他语言,您包含lang
作为查询参数
http://test.com/foo/1/bar/2?lang=De-DE
Then in your custom filter, check if the query parameter is present. 然后在自定义过滤器中,检查查询参数是否存在。 If it is then change culture to match. 如果是,那么改变文化以匹配。 Otherwise use default language. 否则使用默认语言。
Also: You should use 5 character encoding and not 2 char globalization ISO code. 另外:您应该使用5个字符编码而不是2个char全球化ISO代码。 De-DE and not DE or fr-FR and not FR De-DE而不是DE或fr-FR而不是FR
You basically need three things: 你基本上需要三件事:
Html.ActionLink
and/or Url.Action
extension methods). 在应用程序中生成这些URL的辅助方法 ( Html.ActionLink
和/或Url.Action
扩展方法)。 See this answer for further details and code samples. 请参阅此答案以获取更多详细信息和代码示例
(both are written in C# - affiliation disclaimer: I made those samples) (两者都是用C#编写的 - 附属免责声明:我制作了这些样本)
For additional info and further samples you can also read this blog post I wrote on this topic. 有关其他信息和更多示例,您还可以阅读我在此主题上撰写的这篇博客文章 。
http://www.ryadel.com/en/html-actionlink-extension-method-written-c-handle-multi-language-routes-asp-net-mvc/ http://www.ryadel.com/en/html-actionlink-extension-method-written-c-handle-multi-language-routes-asp-net-mvc/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.