ASP.NET Core's ActionFilterAttribute
has these:
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.
However I have a feeling that I can use OnActionExecutionAsync
instead, as it also has an argument of 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? Once I'm done with my stuff, do I simply need to call 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.
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
Asynchronous filters always take precedence over the synchronous filter implementations.
According to the Docs :
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!
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.