'ErrorMessageResourceType'未找到指定的属性。关于 XmlSerialise
在我的 ASP.Net MVC 应用程序中,我有一个模型层,它在业务对象上使用本地化验证注释。
代码如下所示:
[XmlRoot("Item")]
public class ItemBo : BusinessObjectBase
{
[Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(StringResource))]
[HelpPrompt("ItemNumber")]
public long ItemNumber { get; set; }
这效果很好。
当我想将对象序列化为 xml 时,出现错误:
“未找到指定的‘ErrorMessageResourceType’属性”(尽管它在其他错误下丢失,但它是我正在尝试处理的内部异常。
因此问题是使用相关的资源文件位于另一个程序集中,并被标记为“公共”,正如我所说,在我进行序列化之前,一切都正常。
在我的 nunit 测试和目标类中引用了相关的 DataAnnotations 类等。
顺便说一句,HelpPrompt 是我在其他地方定义的另一个数据注释,并且不会导致问题。
此外,如果我将必需属性更改为标准格式,序列化工作正常,
[Required(ErrorMessage="Error")]
有人可以帮助我吗?
In my ASP.Net MVC app I have a Model layer which uses localised validation annotations on business objects.
The code looks like this:
[XmlRoot("Item")]
public class ItemBo : BusinessObjectBase
{
[Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(StringResource))]
[HelpPrompt("ItemNumber")]
public long ItemNumber { get; set; }
This works well.
When I want to serialise the object to xml I get the error:
"'ErrorMessageResourceType' property specified was not found" (although it is lost beneath other errors, it is the innerexception I am trying to work on.
The problem therefore is the use of the DataAnnotations attributes. The relevant resource files are in another assembly and are marked as 'public' and as I said everything works well until I get to serialisation.
I have references to the relevant DataAnnotations class etc in my nunit tests and target class.
By the way, the HelpPrompt is another data annotation I have defined elsewhere and is not causing the problem.
Furthermore if I change the Required attribute to the standard format as follows, the serialisation works ok.
[Required(ErrorMessage="Error")]
Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊哈,答案比我想象的要容易。简而言之,StringResource 程序集中不存在“RequiredField”公共静态属性。
问题是找到错误。序列化对象时,我必须在尝试实例化序列化程序时捕获异常
,然后通过 InnerExceptions 层次结构来分析产生的 InvalidOperationException 并获取准确的错误消息,告诉我出了什么问题:
现在工作正常
Aha, the answer was easier than I expected. In short the "RequiredField" public static property did not exist in the StringResource assembly.
The issue was finding the error. When serialising the object I had to catch the exception on the attempt to instantiate the serialiser
and then work my way dfown through a hierarchy of InnerExceptions to analyse the InvalidOperationException that resulted and get the precise error message which told me what was wrong:
Works ok now