ModelState对null模型有效

时间:2013-11-08 03:59:51

标签: c# asp.net-web-api

我有一个具有必需属性

的Model对象
 public class ApiPing
    {
        [Required]
        public DateTime ClientTime { get; set; }

        public DateTime ServerTime { get; set; }
    }

我有一个控制模型状态的Controller方法。

   public IHttpActionResult Ping(ApiPing model)
    {    
        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        model.ServerTime = DateTime.UtcNow;

        return Ok(model);
    }

如果我向action方法提交了一个正确的请求(带有模型),我会从ModeState.IsValid(true)中获取正确的值。但是,当我提交无效请求(没有模型,因此模型为null)时,我得到一个错误的ModelState.IsValid(也是true)。

我可以简单地检查一下我的代码中的模型是否为null,但这有气味。这是一个预期的“功能”还是ModelState验证中的错误?难道我做错了什么 ?我期待太多了吗?

4 个答案:

答案 0 :(得分:20)

之前我遇到过同样的问题,答案已经在几个论坛上提供,甚至在SO:ModelState.IsValid even when it should not be?

您还可以添加自定义过滤器以验证(无效)缺少的字段和/或空值 http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

http://www.strathweb.com/2012/10/clean-up-your-web-api-controllers-with-model-validation-and-null-check-filters/

答案 1 :(得分:17)

这是一个动作过滤器,用于检查空模型或无效模型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace Studio.Lms.TrackingServices.Filters
{
    public class ValidateViewModelAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext.ActionArguments.Any(kv => kv.Value == null)) {
                actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Arguments cannot be null");
            }

            if (actionContext.ModelState.IsValid == false) {
                actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
            }
        }
    }
}

您可以在全球注册:

config.Filters.Add(new ValidateViewModelAttribute());

或者在课程/动作上按需使用

 [ValidateViewModel]
 public class UsersController : ApiController
 { ...

答案 2 :(得分:4)

  1. 将您的模型声明为结构
  2. 将所有必需属性的类型更改为可以为空的

答案 3 :(得分:1)

我在这里找到了此问题的提示。所以我将在这里给出解决方案。

您如何使用我的解决方案? 您可以在全球范围内注册:

config.Filters.Add(new ValidateModelStateAttribute());

或按需使用它

[ValidateModelState]
public class UsersController : ApiController
{...

或方法

[ValidateModelState]
public IHttpActionResult Create([Required] UserModel data)
{...

如您所见,在方法参数中已放置一个[System.ComponentModel.DataAnnotations.Required]属性。 这表明该模型是必需的,不能为null

您还可以使用自定义消息:

[ValidateModelState]
public IHttpActionResult Create([Required(ErrorMessage = "Custom message")] UserModel data)
{...

这是我的代码:

using System;
using System.Collections.Concurrent;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace your_base_namespace.Web.Http.Filters
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true)]
    public class ValidateModelStateAttribute : ActionFilterAttribute
    {
        private delegate void ValidateHandler(HttpActionContext actionContext);

        private static readonly ConcurrentDictionary<HttpActionBinding, ValidateHandler> _validateActionByActionBinding;

        static ValidateModelStateAttribute()
        {
            _validateActionByActionBinding = new ConcurrentDictionary<HttpActionBinding, ValidateHandler>();
        }

        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            GetValidateHandler(actionContext.ActionDescriptor.ActionBinding)(actionContext);

            if (actionContext.ModelState.IsValid)
                return;

            actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
        }

        private ValidateHandler GetValidateHandler(HttpActionBinding actionBinding)
        {
            ValidateHandler validateAction;

            if (!_validateActionByActionBinding.TryGetValue(actionBinding, out validateAction))
                _validateActionByActionBinding.TryAdd(actionBinding, validateAction = CreateValidateHandler(actionBinding));

            return validateAction;
        }

        private ValidateHandler CreateValidateHandler(HttpActionBinding actionBinding)
        {
            ValidateHandler handler = new ValidateHandler(c => { });

            var parameters = actionBinding.ParameterBindings;

            for (int i = 0; i < parameters.Length; i++)
            {
                var parameterDescriptor = (ReflectedHttpParameterDescriptor)parameters[i].Descriptor;
                var attribute = parameterDescriptor.ParameterInfo.GetCustomAttribute<RequiredAttribute>(true);

                if (attribute != null)
                    handler += CreateValidateHandler(attribute, parameterDescriptor.ParameterName);
            }

            return handler;
        }

        private static ValidateHandler CreateValidateHandler(ValidationAttribute attribute, string name)
        {            
            return CreateValidateHandler(attribute, new ValidationContext(new object()) { MemberName = name });
        }

        private static ValidateHandler CreateValidateHandler(ValidationAttribute attribute, ValidationContext context)
        {
            return new ValidateHandler(actionContext =>
            {
                object value;
                actionContext.ActionArguments.TryGetValue(context.MemberName, out value);

                var validationResult = attribute.GetValidationResult(value, context);
                if (validationResult != null)
                    actionContext.ModelState.AddModelError(context.MemberName, validationResult.ErrorMessage);
            });
        }
    }
}