我想完成两件事:
通过对问题1 使用http://forums.asp.net/t/1862672.aspx/1而MVC 4忽略问题2的DefaultModelBinder.ResourceClassKey,我已经设法让两者都在本地工作.
但是,一旦我发布到网站,"内置"错误消息默认返回到英语,而其他错误消息保持本地化.
我已经阅读了几个应该避免使用App_GlobalResources的地方,但是我不能在不使用它的情况下完成问题1.
我创建了一个名为"WebResources.resx"的.resx文件,将Build Action设置为"Embedded",将Copy to Output Directory设置为"Do no Copy",将Custom Tool设置为"PublicResXFileCodeGenerator"并设置Custom工具命名空间为"资源".项目本身设置为仅发布所需的文件.
我的Global.asax.cs包含以下(相关)代码:
ClientDataTypeModelValidatorProvider.ResourceClassKey = "WebResources";
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(RequiredAttribute),
typeof(MyRequiredAttributeAdapter));
Run Code Online (Sandbox Code Playgroud)
类MyRequiredAttributeAdapter包含以下代码:
public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
public MyRequiredAttributeAdapter(
ModelMetadata metadata,
ControllerContext context,
RequiredAttribute attribute
)
: base(metadata, context, attribute)
{
if (attribute.ErrorMessageResourceType == null)
{
attribute.ErrorMessageResourceType = typeof(Resources.WebResources);
}
if (attribute.ErrorMessageResourceName == null)
{
attribute.ErrorMessageResourceName = "PropertyValueRequired";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是在本地工作但是有没有人知道如何在发布之后让"内置"消息工作?
谢谢您的帮助!
最好的问候,安德烈亚斯