ASP.Net Core从动作过滤器返回

时间:2016-03-30 14:57:08

标签: asp.net-core actionfilterattribute

我需要对请求应用过滤器。我创建了一个ActionFilterAttribute的子类,并覆盖了OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)。我在过滤器内部有一些复杂的逻辑,在某些情况下,我必须停止运行过滤器并返回默认操作结果(就好像没有过滤器一样)。如果我在过滤器中return,工作流似乎完全停止,则不会返回任何操作结果。我尝试await next()或致电base.OnActionExecutionAsync(...),但这并没有奏效。如何实现从过滤器返回? 我有这样的事情:

    public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        //some logic

        if (questionnaireId == Guid.Empty)
        {
            //here I need to let the  mvc action run normally
            return;//it doesn't work
        } else {
            //some logic..
        }            
    }

1 个答案:

答案 0 :(得分:1)

返回默认结果添加await base.OnActionExecutionAsync(context, next);

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
    //some logic

    if (questionnaireId == Guid.Empty)
        await base.OnActionExecutionAsync(context, next);
    else {
        //some logic..
    }
}