asp.net-mvc ASP.NET MVC 资源文件的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1875368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice for ASP.NET MVC resource files
提问by Soe Moe
What are the best usage of the following resource files.
以下资源文件的最佳用法是什么。
- Properties → Resources (Philused this resource for localization in DataAnnotation)
- App_GlobalResources folder
- App_LocalResources folder
- 属性 → 资源(Phil使用此资源在 DataAnnotation 中进行本地化)
- App_GlobalResources 文件夹
- App_LocalResources 文件夹
I also would like to know what is the difference between (1) and (2) in asp.net mvc application.
我也想知道 asp.net mvc 应用程序中 (1) 和 (2) 之间的区别是什么。
采纳答案by Ryan Rivest
You should avoid App_GlobalResourcesand App_LocalResources.
Like Craig mentioned, there are problems with App_GlobalResources/App_LocalResourcesbecause 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.
你应该避免App_GlobalResources和App_LocalResources。就像 Craig 提到的那样,App_GlobalResources/存在问题,App_LocalResources因为您无法在 ASP.NET 运行时之外访问它们。当您对应用程序进行单元测试时,这将成为问题的一个很好的例子。
K. Scott Allen blogged about this a while ago. He does a good job of explaining the problem with App_GlobalResourcesin ASP.NET MVC here.
K. Scott Allen 不久前在博客中谈到了这一点。他App_GlobalResources在这里很好地解释了ASP.NET MVC 中的问题。
回答by marq
If you go with the recommended solution (1) (i.e. as in K. Scott Allen's blog):
如果您使用推荐的解决方案 (1)(即如 K. Scott Allen 的博客中所述):
For those of you trying to use explicit localization expressions (aka declarative resource binding expressions), e.g. <%$ Resources, MyResource:SomeString %>
对于那些尝试使用显式本地化表达式(又名声明性资源绑定表达式)的人,例如 <%$ Resources, MyResource:SomeString %>
public class AppResourceProvider : IResourceProvider
{
private readonly string _ResourceClassName;
ResourceManager _ResourceManager = null;
public AppResourceProvider(string className)
{
_ResourceClassName = className;
}
public object GetObject(string resourceKey, System.Globalization.CultureInfo culture)
{
EnsureResourceManager();
if (culture == null)
{
culture = CultureInfo.CurrentUICulture;
}
return _ResourceManager.GetObject(resourceKey, culture);
}
public System.Resources.IResourceReader ResourceReader
{
get
{
// Not needed for global resources
throw new NotSupportedException();
}
}
private void EnsureResourceManager()
{
var assembly = typeof(Resources.ResourceInAppToGetAssembly).Assembly;
String resourceFullName = String.Format("{0}.Resources.{1}", assembly.GetName().Name, _ResourceClassName);
_ResourceManager = new global::System.Resources.ResourceManager(resourceFullName, assembly);
_ResourceManager.IgnoreCase = true;
}
}
public class AppResourceProviderFactory : ResourceProviderFactory
{
// Thank you, .NET, for providing no way to override global resource providing w/o also overriding local resource providing
private static Type ResXProviderType = typeof(ResourceProviderFactory).Assembly.GetType("System.Web.Compilation.ResXResourceProviderFactory");
ResourceProviderFactory _DefaultFactory;
public AppResourceProviderFactory()
{
_DefaultFactory = (ResourceProviderFactory)Activator.CreateInstance(ResXProviderType);
}
public override IResourceProvider CreateGlobalResourceProvider(string classKey)
{
return new AppResourceProvider(classKey);
}
public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
{
return _DefaultFactory.CreateLocalResourceProvider(virtualPath);
}
}
Then, add this to your web.config:
然后,将其添加到您的 web.config:
<globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" culture="en-US" uiCulture="en"
resourceProviderFactoryType="Vendalism.ResourceProvider.AppResourceProviderFactory" />
回答by Craig Stuntz
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 pagefor global vs. local.
App_* 由 ASP.NET 编译,当您的视图被编译时。它们仅在视图中可用。有关全局与本地的信息,请参阅此页面。

