ASP.NET的Web阿比验证datacontract和DataMember要求 [英] ASP.NET Web Api validation datacontract and datamember required

查看:321
本文介绍了ASP.NET的Web阿比验证datacontract和DataMember要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Web API构建的API,并接收公布数值时,并将其绑定到我的模型,我得到了显得格格不入错误。

我有一个简单的模型完全如下:

 公共类客户端
{
    [ScaffoldColumn(假)]
    [JsonIgnore]
    公众诠释的ClientID {获得;组; }
    [必需的,StringLength(75)]
    公共字符串名称{;组; }
    [需要]
    公共BOOL主动{获得;组; }
}
 

在通过我的控制器发送该模式进入我的帖子方法

 公共对象后([FromBody]客户端postedClient)
 

这是通过X WWW的形式,urlen codeD格式,但它抛出:

 属性活动上键入CreditSearch.Api.Models.Rest.Client是无效的。标记为[必填]值类型属性也必须标有[数据成员(IsRequired = TRUE)]根据需要被认可。考虑归因于声明类型有[DataContract]和[数据成员(IsRequired = TRUE)]属性。
 

我也试了一下发送同样的数据以JSON格式,但我得到了相同的结果。我尝试添加这些属性只是为了获得code工作,但ReSharper的和我无法找到正确的参考。即便如此我preFER不添加,在一个普通的MVC系统验证如果之前没有被要求这样superfluousness属性。

  1. 请我真的需要这些属性?他们不是之前必需的。
  2. 如果这样引用做什么,我需要补充的吗?
解决方案

的原因,这验证没有因为引用类型的成员,只要该成员是反序列化的WebAPI可以检查该成员不为空。对于值类型,不存在空值,所以它是由格式化,检查值是present在请求主体。不幸的是,我们的XML格式不支持[必需]属性,所以它不会提出一个模型状态错误,如果成员丢失。

如果你确定一定的格式化不加模型状态错误的缺失值类型的成员,您可以使用此行删除了验证:

  config.Services.RemoveAll(typeof运算(ModelValidatorProvider),(供应商)=>供应商是InvalidModelValidatorProvider);
 

I'm using the web api to build an API and when receiving the posted values and binding them to my model I get an error that seems out of place.

I have a simple model exactly as below:

public class Client
{
    [ScaffoldColumn(false)]
    [JsonIgnore]
    public int ClientID { get; set; }
    [Required, StringLength(75)]
    public string Name { get; set; }
    [Required]
    public bool Active { get; set; }
}

When sending this model through into my post method on my controller

public object Post([FromBody]Client postedClient)

it goes through the x-www-form-urlencoded formatter but it throws:

Property 'Active' on type 'CreditSearch.Api.Models.Rest.Client' is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be recognized as required. Consider attributing the declaring type with [DataContract] and the property with [DataMember(IsRequired=true)].

I also tried it send the same data in json format but I get the same result. I tried to add these attributes just to get the code working but Resharper and myself cannot find the correct reference. Even so I'd prefer not to add this superfluousness attributes that haven't been required before when validating in a plain MVC system.

  1. Do I really need these attributes? They weren't required before.
  2. If so what references do I need to add?

解决方案

The reason this validation is there is because for reference-typed members, whenever the member is deserialized WebAPI can check that the member is not null. For value types, there is no null value so it's up to the formatter to check that the value is present in the request body. Unfortunately, our XML formatter doesn't support the [Required] attribute so it won't raise a model state error if the member is missing.

If you're OK with certain formatters not raising model state errors for the missing value-typed members, you can use this line to remove the validation:

config.Services.RemoveAll(typeof(ModelValidatorProvider), (provider) => provider is InvalidModelValidatorProvider);

这篇关于ASP.NET的Web阿比验证datacontract和DataMember要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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