ASP.NET Core的ActionFilterAttribute中的异步OnActionExecuting

gro*_*kky 19 c# action-filter asp.net-core

ASP.NET Core ActionFilterAttribute有这些:

public virtual void OnActionExecuting(ActionExecutingContext context);
public virtual void OnActionExecuted(ActionExecutedContext context);
public virtual Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next);
Run Code Online (Sandbox Code Playgroud)

我需要一个OnActionExecuting不存在的异步版本.

但是我有一种感觉,我可以使用OnActionExecutionAsync,因为它也有一个参数ActionExecutingContext.

我是否正确,尽管名称,他们在过程中的同一点触发?

另外,我需要对next论证做些什么?一旦我完成了我的东西,我只需要打电话await next()吗?

是吗?我不确定,因为我找不到这方面的文档.

jer*_*enh 40

异步过滤器的工作方式略有不同:首先执行必须在操作之前执行的代码,调用next()实际逻辑,最后添加要在操作之后执行的代码.

public async Task OnActionExecutionAsync(ActionExecutingContext context, 
                                         ActionExecutionDelegate next)
{

    // logic before action goes here

    await next(); // the actual action

    // logic after the action goes here
}
Run Code Online (Sandbox Code Playgroud)

文档在这里:https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters#implementation