C# 如何让ModelState在asp.net mvc web api上工作?
这是我第一次使用asp.net mvc webApi,我有Post/Put方法,其中有一个名为ProductViewModel的参数。此ViewModel的某些属性具有要验证的数据批注,例如C# 如何让ModelState在asp.net mvc web api上工作?,c#,asp.net-mvc,rest,asp.net-web-api,modelstate,C#,Asp.net Mvc,Rest,Asp.net Web Api,Modelstate,这是我第一次使用asp.net mvc webApi,我有Post/Put方法,其中有一个名为ProductViewModel的参数。此ViewModel的某些属性具有要验证的数据批注,例如必需的,字符串长度等。。。我有这样的post方法: public HttpResponseMessage Post([FromBody] ProductViewModel value) { if (ModelState.IsValid) { // persist data here..
必需的,字符串长度等。。。我有这样的post方法:
public HttpResponseMessage Post([FromBody] ProductViewModel value)
{
if (ModelState.IsValid)
{
// persist data here...
return Request.CreateResponse(HttpStatusCode.OK);
}
return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors());
}
public class ProductViewModel
{
[Display(ResourceType = typeof(Resources.Global), Name = "Name")]
[Required(ErrorMessageResourceType = typeof(Resources.Global), ErrorMessageResourceName = "Required")]
[StringLength(100, ErrorMessageResourceType = typeof(Resources.Global), ErrorMessageResourceName = "Range")]
public string Name { get; set; }
[Display(ResourceType = typeof(Resources.Global), Name = "ShortName")]
[Required(ErrorMessageResourceType = typeof(Resources.Global), ErrorMessageResourceName = "Required")]
[StringLength(20, ErrorMessageResourceType = typeof(Resources.Global), ErrorMessageResourceName = "Range")]
public string ShortName { get; set; }
}
我使用GetErrors()
方法作为扩展,以获取错误的列表并传递给客户端。我的问题是:为什么ModelState不起作用
如果我将null传递到ViewModel的属性中,则此验证根本不起作用IsValid
属性始终为true
。有没有办法解决这个问题,让ModelState工作,比如MVC
我的模型如下所示:
public HttpResponseMessage Post([FromBody] ProductViewModel value)
{
if (ModelState.IsValid)
{
// persist data here...
return Request.CreateResponse(HttpStatusCode.OK);
}
return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors());
}
public class ProductViewModel
{
[Display(ResourceType = typeof(Resources.Global), Name = "Name")]
[Required(ErrorMessageResourceType = typeof(Resources.Global), ErrorMessageResourceName = "Required")]
[StringLength(100, ErrorMessageResourceType = typeof(Resources.Global), ErrorMessageResourceName = "Range")]
public string Name { get; set; }
[Display(ResourceType = typeof(Resources.Global), Name = "ShortName")]
[Required(ErrorMessageResourceType = typeof(Resources.Global), ErrorMessageResourceName = "Required")]
[StringLength(20, ErrorMessageResourceType = typeof(Resources.Global), ErrorMessageResourceName = "Range")]
public string ShortName { get; set; }
}
谢谢。能否确保您在请求中传递了内容类型?(如果未传递内容类型,则会设置特定类型的默认值,并且模型状态不会有错误…此错误最近已修复)
此外,您还可以执行以下操作:
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState)
是的,我已经通过Fiddler传递了内容类型:application/json
,对吗?我用属性构造并填充了我的对象ProductViewModel
,但ModelState.IsValid始终为true,不管是否有属性REquired为null,IsValid
始终为true。这是asp.net mvc webapi的一个缺陷吗?内容类型看起来不错…你能分享一下你的模型是什么样子吗?还有可能是来自Fiddler的原始请求吗?我编辑了我的帖子,看看我的模型,只有两个属性和一些验证。我通过fiddler传递的jSon是:{Name:null,ShortName:”}
,只是为了测试验证,但它确实验证了,我的ORM工具(nhibernate)还有其他异常。嗯,我无法重新编写此场景。我有以下内容:POST HTTP/1.1用户代理:Fiddler内容类型:application/json主机:kirandesktop:9095内容长度:27{Name:null,ShortName:}响应:{“消息”:“请求无效”,“模型状态”:{“value.Name”:[“Name字段是必需的”],“value.ShortName:[“ShortName字段是必需的”]}我会尽力为你演示我的场景。我有一个ModelStateWrapperApi
,这个类使用我的控制器的ModelState来进行验证。我现在看到使用ModelState.IsValid是commingfalse
,这是可以的,但是当我在我的业务类中的控制器的构造函数中初始化ModelStateWrapperApi时,它不起作用,我不知道为什么,因为我知道这些类是引用类型,因此,我在包装器中有ModelStateDictionary的引用,但在那里,它是有效的。为什么?我可以在构造函数上初始化它并在谓词方法上使用它吗?(投递)?