如何本地化 ASP.NET MVC 应用程序中的控制器名称和操作?
我想完全本地化我的 ASP.NET MVC 应用程序,虽然有很多文章讨论 ASP.NET MVC 本地化的各种陷阱,但没有一篇涉及我想要处理的方面,那就是URL 的本地化,包括控制器名称和操作。
我想要一种方法来指定应该出现的字符串,而不是 URL 中的控制器名称和操作名称,例如在资源文件中。
这如何以理智的方式实现?预先感谢您的帮助!
编辑:
我仍在寻找一种方法来做到这一点。欢迎任何帮助。
是否有任何框架或库已经实现了这个?
I'd like to fully localize my ASP.NET MVC application, and while there are many articles that deal with the various pitfalls of ASP.NET MVC localization, none of them deals with an aspect that I want to deal with, and that is the localization of URLs, including the controller names and actions.
I would like a way to specify the strings that should appear instead of the controller name and action name in the URL, for example in a resource file.
How is this doable in a sane way? Thanks in advance for your help!
EDIT:
I'm still looking for a way to do this. Any help would be welcome.
Is there any framework or lib implementing this already?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我以前从未这样做过,但是您不能为您想要支持的每种语言添加一个条目到路由表中吗?所有本地化路线都可以重定向到您选择的语言的路线。查看这些相关问题:
使用 ASP.NET MVC 的多语言网站
我应该如何使用 ASP.NET MVC 路由实现本地化?
I've never done this before, but couldn't you add an entry to the routes table for every language you want to support? All of the localized routes could redirect to the route in the language of your choice. Check out these related questions:
Multi-lingual websites with ASP.NET MVC
How should I implement localization with ASP.NET MVC routes?
简短的回答:不。
URL 应该是通用的,因此区域设置不变。这就是为什么我们不支持在模型绑定期间本地化控制器、操作或查询字符串值。
Short answer: don't.
URLs are supposed to be universal and thus locale-invariant. This is why we don't support localizing controllers, actions, or query string values during model binding.
看看这个简单的解决方案:
翻译路线(ASP.NET MVC 和 Webforms)
您还可以尝试很棒的 AttributeRouting 项目!您可以通过 NuGet 获取它。
Take a look at this simple solution:
Translating routes (ASP.NET MVC and Webforms)
You can also try the awesome AttributeRouting project that I just found! You can get it through NuGet.
请注意,本地化操作和控制器名称时尚未完成。
您仍然需要本地化所有路由参数。这对于枚举来说尤其痛苦。
即 www.seller.com/catalogue/list/winterclothing www.seller.com/catalogue/list/summerclothing 其中
winterclothing
和summerclothing
将是枚举。我想说:有可能,但不可行。
Take into account that you are not finished when you localized action and controller names.
You would still need to localize any routing parameters. That's especially painfull for enums.
Ie www.seller.com/catalogue/list/winterclothing www.seller.com/catalogue/list/summerclothing where
winterclothing
andsummerclothing
would be enums.I would say: It's possible but not doable.