我有一个 MVC Web 应用程序,它使用资源文件的自定义文件夹和命名空间。我需要能够从资源文件中提取后期绑定(bind)值,即使用资源类型和字符串。我尝试使用:
HttpContext.GetGlobalResourceObject("ResourceClass", "SomeKey")
但是返回 null。当我将资源移动到 App_GlobalResources 时,这可以工作,但会引发其他问题。我尝试解决这些问题,但它们似乎比回到我最初的计划(仅能够从自定义文件夹中的资源文件中读取)更深入、数量更多。
简而言之,我正在尝试本地化我的 xVal 验证,包括错误消息以及正则表达式验证器的模式。我已经把一切都做好了,除了这一件,我正在尝试本地化模式。由于它没有内置到 DataAnnotations.RegularExpressionAttribute 中,因此我需要根据属性中提供的资源类型和名称自行获取它。这就是我的困境。
我可以使用其他方法获取资源值吗?或者,我的资源文件必须位于 App_GlobalResources 文件夹中吗?如果是后者,那么我将需要针对所有其他问题展开另一次讨论,或者为正则表达式内容实现某种更粗略的本地化形式。
提前致谢。
杰拉德
最佳答案
这是我们提出的解决方案:
public static class ResourceHelper
{
public static string GetString(Type resourceType, string resourceName)
{
return new ResourceManager(resourceType.FullName, resourceType.Assembly)
.GetString(resourceName);
}
public static string GetString(Type resourceType, string resourceName, CultureInfo culture)
{
return new ResourceManager(resourceType.FullName, resourceType.Assembly)
.GetString(resourceName, culture);
}
public static object GetObject(Type resourceType, string resourceName)
{
return new ResourceManager(resourceType.FullName, resourceType.Assembly)
.GetObject(resourceName);
}
public static object GetObject(Type resourceType, string resourceName, CultureInfo culture)
{
return new ResourceManager(resourceType.FullName, resourceType.Assembly)
.GetObject(resourceName, culture);
}
}
关于asp.net - *无需*使用 GetGlobalResourceObject 获取资源值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2293572/