如何检查Validation Web API [英] how to check Validation Web API

查看:96
本文介绍了如何检查Validation Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

如何检查数据字典属性的验证,例如使用ASP.NET Web API中的模型验证检查名字和姓氏是否为空或长度> 0? >


Dear All,
how can i check validaion on data dictionary attributes for example check if Firstname and last name is not null or length>0 using Model Validation in ASP.NET Web API

public HttpResponseMessage Post([FromBody] List<Dictionary<string,string>> _lstDicobject)
      {
//JsonConvert.DeserializeObject<Person>(JsonConvert.SerializeObject(kvpList)) object (person) data type
return JsonConvert.DeserializeObject<Person>(JsonConvert.SerializeObject(kvpList)).sendMail();
     }



人员类




where person class

class person {
public string Firstname {set ; get;}
public string lastname {set ; get;}
public string send mail()
{
return Firstname+" "+ lastname;
}
}

推荐答案

参考 - ASP.NET Web API中的模型验证 [ ^ ] 。

Refer - Model Validation in ASP.NET Web API[^].
if (ModelState.IsValid)

这就是你能做的。



在模型类上,用适当的属性声明变量。与not null一样,您可以使用 [Required] 属性来标记它。

This is what you can do.

On Model Class, declare your variable with proper attributes. Like for not null, you can mark it with [Required] attribute.


这篇关于如何检查Validation Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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