Home > front end >  OnActionExecutedAsync Not available in ActionFilterAttribute in .Net core
OnActionExecutedAsync Not available in ActionFilterAttribute in .Net core

Time:02-11

I am porting over a action filter attribute from .net framework 4.6 to .net core 3.1 . One thing i noticed in .net core ActionFilterAttribute is that it , in .net core ther is no OnActionExecutedAsync Method. Methods in ActionFilterAttribute :-

public virtual void OnActionExecuted(ActionExecutedContext context);
public virtual void OnActionExecuting(ActionExecutingContext context);
public virtual Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next);
public virtual void OnResultExecuted(ResultExecutedContext context);
public virtual void OnResultExecuting(ResultExecutingContext context);
public virtual Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next); 

I have a couple of async methods that need to executed after the execution of the action . What would be the best practice to do this.

CodePudding user response:

Use OnActionExecutionAsync, where you can invoke the next delegate and then run your own async logic after:

public override async Task OnActionExecutionAsync(
    ActionExecutingContext context, ActionExecutionDelegate next)
{
    var actionExecutedContext = await next();

    // .. Your awaits here.
}

The next delegate returns the ActionExecutedContext.

  • Related