简体   繁体   English

ASP.NET Core自定义验证属性未触发

[英]ASP.NET Core Custom Validation Attribute Not Firing

I have a GET method in API Controller. 我在API Controller中有一个GET方法。 I would like that method to be validated using custom validation attribute as below. 我希望使用自定义验证属性来验证该方法,如下所示。 However it's not getting fired for some reasons. 但是由于某些原因,它没有被解雇。

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class CheckValidRoute : ValidationAttribute
    {
        private readonly string _apiRoute;
        private readonly string _operation;

        public override bool RequiresValidationContext { get { return true; } }

        public CheckValidRoute(string apiRoute, string operation)
        {
            _apiRoute = apiRoute;
            _operation = operation;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
           //Validation logic here
        }
    }

Controller 调节器

public class TestController : ControllerBase
    {
        [HttpGet("production/{movieId}/Test")]
        [ProducesResponseType(typeof(ResponseModel<string>), 200)]
        [Authorize(Policy = SecurityConstants.PseudofilmAuthorizationPolicy)]
        [CheckValidRoute("production/{movieId}/Test", "GET")]
        public async Task<ResponseModel<string>> TestGet(long movieId)
        {
            return ResponseModelHelper.BuildResponse("Success", $"production/{movieId}/Test", "Test");
        }
    }

I am not sure what I am missing here. 我不确定我在这里缺少什么。

-Alan- -Alan-

You are using the wrong base class to implement your attribute. 您使用错误的基类来实现您的属性。 ValidationAttribute is used on Models to validate their property values. Models上使用ValidationAttribute来验证其属性值。 An example of this is the Required attribute. 一个示例是Required属性。

In your case you want to implement an ActionFilter , which can be applied to a controller method and can perform validation before the method is called. 在您的情况下,您想实现一个ActionFilter ,它可以应用于控制器方法,并且可以在调用该方法之前执行验证。 An example for you would be: 一个适合您的示例是:

public class CheckValidRouteAttribute : ActionFilterAttribute
{
    private readonly string _apiRoute;
    private readonly string _operation;

    public CheckValidRouteAttribute(string apiRoute, string operation) : base()
    {
        _apiRoute = apiRoute;
        _operation = operation;
    }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var request = context.HttpContext.Request;

        var method = request.Method;

        if (string.Compare(method, _operation, true) != 0)
        {
            context.Result = new BadRequestObjectResult("HttpMethod did not match");
        }
    }
}

In the OnActionExecuting method you can perform your checks against the HttpContext.Request object. OnActionExecuting方法中,您可以对HttpContext.Request对象执行检查。 I have left that as an exercise for you as I don't know your exact requirements :-) 我把它留给您作为练习,因为我不知道您的确切要求:-)

After applying this attribute to your controller method, the OnActionExecuting will be called before the controller method is called so you can do your validation. 在将此属性应用于控制器方法之后,将在调用控制器方法之前调用OnActionExecuting ,以便您可以进行验证。

You can read more about ActionFilters here 您可以在此处阅读有关ActionFilters的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM