【问题标题】:ASP.Net Core Returning from action filter从动作过滤器返回的 ASP.Net Core
【发布时间】:2016-03-30 14:57:08
【问题描述】:

我需要对请求应用过滤器。我创建了一个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..
        }            
    }

【问题讨论】:

  • 你能发布你的代码吗?还有为什么要实现一个ActionFilter,可能一个middleware是最好的选择
  • @aguafrommars 我怀疑中间件,因为我的逻辑只处理 MVC,我不想在整个应用程序的管道中再添加一个处理程序

标签: asp.net-core actionfilterattribute


【解决方案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..
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 2019-03-18
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2011-02-21
    相关资源
    最近更新 更多