0

我有两个过滤器:

public class ValidateModelStateFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.ModelState.IsValid == false)
        {
            actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
}

public class AuthorizeRequestAttribute : Attribute, IAuthorizationFilter
{
    public bool AllowMultiple
    {
        get { return false; }
    }

    public async Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
    {
        // TODO: validate the request

        return await continuation();
    }
}

第一个在Global.asax Application_Start方法中注册:

protected void Application_Start()
{
    GlobalConfiguration.Configuration.Filters.Add(new ValidateModelStateFilter());
}

第二个添加到控制器操作中:

[HttpGet]
[AuthorizeRequestAttribute]
public string GetValue(GetValueRequest request)
{
    return _service.GetValue(request.Product_Id);
}

一切正常,除了在过滤器[AuthorizeRequestAttribute]之前执行的事实。[ValidateModelStateFilter]

我怎样才能[ValidateModelStateFilter]在任何其他过滤器之前执行?

4

1 回答 1

0

IAuthorizationFilter 将始终首先执行。您可以选择在实现 iAuthorizationFilter 的方法中执行 ValidationModelStateFilter 逻辑,从而确保它首先运行。无论如何,您可能已经在这个阶段解决了您的问题。

于 2014-01-29T10:31:28.357 回答