简体   繁体   English

ASP.NET Core 的 ActionFilterAttribute 中的异步 OnActionExecuting

[英]Async OnActionExecuting in ASP.NET Core's ActionFilterAttribute

ASP.NET Core's ActionFilterAttribute has these: ASP.NET Core 的ActionFilterAttribute有这些:

public virtual void OnActionExecuting(ActionExecutingContext context);
public virtual void OnActionExecuted(ActionExecutedContext context);
public virtual Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next);

I need an async version of OnActionExecuting , which doesn't exist.我需要一个不存在的OnActionExecuting异步版本。

However I have a feeling that I can use OnActionExecutionAsync instead, as it also has an argument of ActionExecutingContext .但是我有一种感觉,我可以使用OnActionExecutionAsync代替,因为它也有一个ActionExecutingContext参数。

Am I correct that despite the name, they trigger at the same point in the process?尽管名称不同,但它们在过程中的同一点触发,我是否正确?

Also, what do I need to do with the next argument?另外,我需要对next论点做什么? Once I'm done with my stuff, do I simply need to call await next() ?一旦我完成了我的工作,我是否只需要调用await next()

Is that it?是这样吗? I'm unsure as I can't find docs for this.我不确定,因为我找不到这方面的文档。

Asynchronous filters work a bit differently: first execute code that must be executed before the action, call next() for the actual logic, finally add code to be executed after the action.异步过滤器的工作方式略有不同:首先执行必须在动作之前执行的代码,为实际逻辑调用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
}

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

Asynchronous filters always take precedence over the synchronous filter implementations.异步过滤器始终优先于同步过滤器实现。

According to the Docs :根据文档

  • It's advised to implement either the synchronous or the async version of a filter interface, not both.建议实现过滤器接口的同步或异步版本,而不是两者。 The runtime checks first to see if the filter implements the async interface, and if so, it calls that.运行时首先检查过滤器是否实现了异步接口,如果是,它会调用它。 If not, it calls the synchronous interface's method(s).如果没有,它调用同步接口的方法。 If both asynchronous and synchronous interfaces are implemented in one class, only the async method is called.如果在一个类中同时实现了异步和同步接口,则只调用异步方法。

However, you can manage to have both.但是,您可以设法同时拥有两者。 For instance:例如:

public class TimestampFilter : IActionFilter, IAsyncActionFilter 
{    
    public void OnActionExecuting(ActionExecutingContext context)    
    {         
        context.ActionDescriptor.RouteValues["timestamp"] = DateTime.Now.ToString();    
    }

    public void OnActionExecuted(ActionExecutedContext context)    
    {         
        var ts = DateTime.Parse(context.ActionDescriptor. RouteValues["timestamp"]).AddHours(1).ToString();        
        context.HttpContext.Response.Headers["X-EXPIRY-TIMESTAMP"] = ts;    
    }

     public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)    
    {        
        this.OnActionExecuting(context);        
        var resultContext = await next();
        this.OnActionExecuted(resultContext);    
    }
 }

An ever better pattern:一个更好的模式:

public override async Task OnActionExecutionAsync(
    ActionExecutingContext context, 
    ActionExecutionDelegate next)
{
    try
    {
          //do your async things here                 
    }
    finally
    {
        await base.OnActionExecutionAsync(context, next); <--- notice this!
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的ASP.NET Web API ActionFilterAttribute OnActionExecuting无法启动? - Why is my ASP.NET Web API ActionFilterAttribute OnActionExecuting not firing? ASP.NET CORE OnActionExecuting 不远程触发 - ASP.NET CORE OnActionExecuting Not Firing Remotely ASP.net 内核中 ActionFilterAttribute 的替代方案 - Alternative to ActionFilterAttribute in ASP.net core 在控制器的 OnActionExecuting 之前执行全局过滤器,在 ASP.NET Core 中 - Execute global filter before controller's OnActionExecuting, in ASP.NET Core ASP MVC ActionFilterAttribute OnActionExecuting 未触发 - ASP MVC ActionFilterAttribute OnActionExecuting not fired ASP.NET Core 的 ChangeToken.OnChange 中的异步操作 - Async action in ASP.NET Core's ChangeToken.OnChange ASP.NET Core中的异步和异步合成 - async and async composition in ASP.NET Core ASP.NET MVC-路由和ActionFilterAttribute - ASP.NET MVC - Routing and ActionFilterAttribute 如何在 IActionFilter Asp.net core 中读取 OnActionExecuting(ActionExecutingContext context) 中的请求正文 - How to read request body in OnActionExecuting(ActionExecutingContext context) in IActionFilter Asp.net core ASP.NET MVC3 + ActionFilterAttribute +注入? - ASP.NET MVC3 + ActionFilterAttribute + Injection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM