要求XML请求对象具有值类型 [英] Require an XML request object to have a value type

查看:52
本文介绍了要求XML请求对象具有值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在Web API中,是否可以强制XML请求对象具有值类型?



[必需]不适用于值类型。



我已经尝试了属性本身的以下属性而没有运气。还有另一种方法吗?我正在寻找一个可以轻松处理许多不同对象的解决方案 - 一个属性本来就很棒。

 

[ 必需 ]
[ DataMember IsRequired = true )]
[ XmlElement IsNullable = )]
[ JsonProperty 必需 = 必需 AllowNull )]
[ DataMember XmlText ]

......

//在示例中,Test1应该是必需的
公开 RequestModel1
{
公开 int Test1 { 得到 ; 设置 ; }
}



解决方案

Hello Joseph,


>> [必需]不起作用值类型。


对于值类型,不可能没有值。换句话说,"requried"属性是使用  <会更好。 strong> RangeAttribute [范围(最小值,
max,ErrorMessage)]值类型。


>>我正在寻找一种可以轻松处理许多不同对象的解决方案 - 一个属性本来就很棒。


根据您的条件,您应该创建  客户验证属性 。据我所知,一个属性可以处理
的值类型,而.net框架中不存在引用类型。


我认为你应该发布一个帖子给  ASP.NET论坛以获得适当的支持。有很多经验可以帮助您快速解决问题。


此致,


feih


In Web API, is it possible to force an XML request object to have a value type?

[Required] doesn't work on value types.

I have tried the following attributes on the property itself with no luck. Is there another way to do this? I am looking for a solution that will easily work on many different objects - an attribute would have been great.

[Required] [DataMember(IsRequired = true)] [XmlElement(IsNullable = false)] [JsonProperty(Required = Required.AllowNull)] [DataMember, XmlText] ...

//in example, Test1 should be required public class RequestModel1 { public int Test1 { get; set; } }


解决方案

Hello Joseph,

>>[Required] doesn't work on value types.

For value type ,it's impossible to not have a value . In other words , the "requried" attribute is effectively meaningless. It would be better to use RangeAttribute[Range(min, max, ErrorMessage )] for value type.

>>I am looking for a solution that will easily work on many different objects - an attribute would have been great.

For your criteria , you should create a customer validation attribute . As far as i know, an attribute could handle for value type and reference type doesn't exist in .net framework.

And I think you should post a thread to ASP.NET forum for suitable support . there are a lot of experience people that could help you work out the problem quickly.

Sincerely,

feih


这篇关于要求XML请求对象具有值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆