ASP.NET MVC 资源文件的最佳实践
以下资源文件的最佳用途是什么?
- 属性 → 资源(Phil 使用此资源进行本地化在DataAnnotation中)
- App_GlobalResources文件夹
- App_LocalResources文件夹
我还想知道asp.net mvc应用程序中(1)和(2)之间有什么区别。
What are the best usage of the following resource files.
- Properties → Resources (Phil used this resource for localization in DataAnnotation)
- App_GlobalResources folder
- App_LocalResources folder
I also would like to know what is the difference between (1) and (2) in asp.net mvc application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该避免
App_GlobalResources
和App_LocalResources
。正如 Craig 提到的,
App_GlobalResources
/App_LocalResources
存在问题,因为您无法在 ASP.NET 运行时之外访问它们。一个很好的例子说明了当您对应用程序进行单元测试时会出现什么问题。K. Scott Allen 不久前在博客中谈到了这一点。他很好地解释了 ASP.NET MVC 中的
App_GlobalResources
问题 此处。You should avoid
App_GlobalResources
andApp_LocalResources
.Like Craig mentioned, there are problems with
App_GlobalResources
/App_LocalResources
because you can't access them outside of the ASP.NET runtime. A good example of how this would be problematic is when you're unit testing your app.K. Scott Allen blogged about this a while ago. He does a good job of explaining the problem with
App_GlobalResources
in ASP.NET MVC here.如果您采用推荐的解决方案 (1)(即,如 K. Scott Allen 的博客中所示):
对于那些尝试使用显式本地化表达式(又名声明性资源绑定表达式)的人,例如
<%$ Resources, MyResource :SomeString %>
然后,将其添加到您的 web.config 中:
If you go with the recommended solution (1) (i.e. as in K. Scott Allen's blog):
For those of you trying to use explicit localization expressions (aka declarative resource binding expressions), e.g.
<%$ Resources, MyResource:SomeString %>
Then, add this to your web.config:
属性 → 资源可以在视图之外看到,并且在编译应用程序时会生成强类型。
当您的视图被编译时,App_* 由 ASP.NET 编译。它们仅在视图中可用。请参阅此页面了解全局与本地。
Properties → Resources can be seen outside of your views and strong types are generated when you compile your application.
App_* is compiled by ASP.NET, when your views are compiled. They're only available in the view. See this page for global vs. local.