从 OnActionExecutionAsync 返回而不执行 asp.net core 中的操作

Tuf*_*and 5 c# custom-action-filter action-filter onactionexecuting asp.net-core-mvc

这里我想从 the 返回custom action filter而不执行controller action methodin asp.net core WEB API

以下是我对样品的要求code

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
    bool valid=SomeMethod();
    if(valid)
        //executes controller action
    else
        //returns without executing controller action with the custom message (if possible)
}
Run Code Online (Sandbox Code Playgroud)

我搜索并找到了一些相关的问题和答案,但没有任何对我有用。

找到了这个await base.OnActionExecutionAsync(context, next);,但它跳过了剩余的逻辑filters并直接执行,controller action所以对我的场景不起作用。

Tuf*_*and 12

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
    bool valid=SomeMethod();
    if(valid)
         await next();
    else
        context.Result = new BadRequestObjectResult("Invalid!");
}
Run Code Online (Sandbox Code Playgroud)