使用ActionFilterAttribute进行模型验证

时间:2015-08-20 16:18:09

标签: c# validation enterprise-library

我正在尝试使用ActionFilterAttribute在调用Web API时验证模型。我添加了以下内容

public class ValidateModelStateAttribute : ActionFilterAttribute
{
   public override void OnActionExecuting(HttpActionContext actionContext)
   {
       if (!actionContext.ModelState.IsValid)
       {
           actionContext.Response = actionContext.Request.CreateErrorResponse(
       HttpStatusCode.BadRequest, actionContext.ModelState);
            }
        }
    }

我已将标准验证器添加到我的模型属性中,一切正常。但是,当我添加自定义类级别验证器时,它会失败,并显示以下错误。

  

XXXValidatorAttribute类型的验证属性不能用于   验证价值。"

     

StackTrace:at   Microsoft.Practices.EnterpriseLibrary.Validation.Validators.BaseValidationAttribute.IsValid(对象   价值)\ r \ n at   System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(对象   value,ValidationContext validationContext)\ r \ n at   System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(对象   value,ValidationContext validationContext)\ r \ n at   System.Web.Http.Validation.Validators.DataAnnotationsModelValidator.Validate(ModelMetadata   元数据,对象容器)\ r \ n at   System.Web.Http.Validation.DefaultBodyModelValidator.ShallowValidate(ModelMetadata   元数据,ValidationContext validationContext,Object容器,   IEnumerable' 1验证器)\ r \ n at   System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata   元数据,ValidationContext validationContext,Object容器,   IEnumerable' 1验证器)\ r \ n at   System.Web.Http.Validation.DefaultBodyModelValidator.Validate(对象   model,Type type,ModelMetadataProvider metadataProvider,   HttpActionContext actionContext,String keyPrefix)\ r \ n at   System.Web.Http.ModelBinding.FormatterParameterBinding.d__0.MoveNext(个)\ r \ n ---   从抛出异常的先前位置开始的堆栈跟踪结束   --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()\ r \ n at   System.Web.Http.Controllers.HttpActionBinding.d__0.MoveNext(个)\ r \ n ---   从抛出异常的先前位置开始的堆栈跟踪结束   --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()\ r \ n at   System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext(个)\ r \ n ---   从抛出异常的先前位置开始的堆栈跟踪结束   --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter' 1.GetResult()\ r \ n at   System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext(个)\ r \ n ---   从抛出异常的先前位置开始的堆栈跟踪结束   --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\ r \ n at   System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

当我使用验证工厂通过单元测试调用它时,此验证器工作正常。我不确定为什么它在ModelState验证期间失败了?这两者有什么区别?

我将模型定义为

我有一个定义为

的模型
[CashDetailValidator()]
public sealed class CashDetails : DetailsBase
{
    // Reference Number
    [ValidatorComposition(CompositionType.Or)]
    [NotNullValidator(Negated=true)]
    [StringLengthValidator(1, RangeBoundaryType.Inclusive, 16, RangeBoundaryType.Exclusive, MessageTemplate = "Reference number should be between 1 to 16 characters in length.")]
    public string RelatedReference { get; set; } ...........................

}}

组合验证器也会出现同样的错误。

0 个答案:

没有答案