ASP.NET MVC 资源文件的最佳实践

发布于 2024-08-14 06:46:50 字数 308 浏览 7 评论 0原文

以下资源文件的最佳用途是什么?

  1. 属性 → 资源(Phil 使用此资源进行本地化在DataAnnotation中)
  2. App_GlobalResources文件夹
  3. App_LocalResources文件夹

我还想知道asp.net mvc应用程序中(1)和(2)之间有什么区别。

What are the best usage of the following resource files.

  1. Properties → Resources (Phil used this resource for localization in DataAnnotation)
  2. App_GlobalResources folder
  3. App_LocalResources folder

I also would like to know what is the difference between (1) and (2) in asp.net mvc application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨敛清荷 2024-08-21 06:46:50

您应该避免 App_GlobalResourcesApp_LocalResources
正如 Craig 提到的,App_GlobalResources/App_LocalResources 存在问题,因为您无法在 ASP.NET 运行时之外访问它们。一个很好的例子说明了当您对应用程序进行单元测试时会出现什么问题。

K. Scott Allen 不久前在博客中谈到了这一点。他很好地解释了 ASP.NET MVC 中的 App_GlobalResources 问题 此处

You should avoid App_GlobalResources and App_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.

素手挽清风 2024-08-21 06:46:50

如果您采用推荐的解决方案 (1)(即,如 K. Scott Allen 的博客中所示):

对于那些尝试使用显式本地化表达式(又名声明性资源绑定表达式)的人,例如 <%$ 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);
    }
}

然后,将其添加到您的 web.config 中:

    <globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" culture="en-US" uiCulture="en"
                   resourceProviderFactoryType="Vendalism.ResourceProvider.AppResourceProviderFactory" />

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 %>

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:

    <globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" culture="en-US" uiCulture="en"
                   resourceProviderFactoryType="Vendalism.ResourceProvider.AppResourceProviderFactory" />
把梦留给海 2024-08-21 06:46:50

属性 → 资源可以在视图之外看到,并且在编译应用程序时会生成强类型。

当您的视图被编译时,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文