AsyncActionFilter in ASP.NET CORE MVC
IAsyncActionFilter filter is an async version of Action filter. This filter is a bit different than the sync version where we had 2 method one executes before the action method is invoked and another action method which gets invoke after the action method is executed.
In the Async filter, we have only one method OnActionExecutionAsync where we can do both task one before executing the action method and another after execution of ActionMethod.
Let’s Create Custom Action Filter with name ‘CustomAsyncActionFilter‘ and then we are going to inherit with Attribute, IAsyncActionFilter and implement OnActionExecutionAsync method.
Implementation
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Threading.Tasks;
namespace WebApplication4.Filters
{
public class CustomAsyncActionFilter : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
// Do something before the action executes.
// next() calls the action method.
var resultContext = await next();
// resultContext.Result is set.
// Do something after the action executes.
}
}
}
In OnActionExecutionAsync method logic before the execution of the action method, we are going to check Session which has UserRole Value in it. if the value is null then we will redirect it to an error page and another condition is If Session Value does not contain Admin role still, they will redirect it to an error page. while calling (await next();) executes the action method and logic after the execution of action method is called where we are looking for any errors and we are handling it and redirecting it to Error Controller.
Code Snippet of CustomAsyncActionFilter
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using System;
using System.Threading.Tasks;
namespace WebApplication4.Filters
{
public class CustomAsyncActionFilter : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
string currentUserRole = Convert.ToString(context.HttpContext.Session.GetString("UserRole"));
if (!string.IsNullOrEmpty(currentUserRole))
{
if (currentUserRole != "Admin")
{
context.Result = new RedirectToRouteResult
(
new RouteValueDictionary(new
{
action = "Action",
controller = "Controller"
}));
return;
}
else
{
context.Result = new RedirectToRouteResult
(
new RouteValueDictionary(new
{
action = "Action",
controller = "Controller"
}));
return;
}
}
// next() calls the action method.
ActionExecutedContext resultContext = await next();
// resultContext.Result is set.
// Do something after the action executes.
if (resultContext.Exception != null)
{
resultContext.ExceptionHandled = true;
resultContext.Result = new RedirectToRouteResult
(
new RouteValueDictionary(new
{
action = "Error",
controller = "Error"
}));
}
}
}
}
How to Apply Filter on Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication4.Filters;
namespace WebApplication4.Controllers
{
[TypeFilter(typeof(CustomAsyncActionFilter))]
public class DefaultController : Controller
{
public IActionResult Index()
{
throw new NotImplementedException();
return View();
}
}
}