In general, filters can be implemented by extending the built-in types provided by ASP.NET Core. Let's walk through a simple declaration of a custom action filter:
using Microsoft.AspNetCore.Mvc.Filters;
namespace SampleAPI.Filters
{
public class CustomActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
// do something before the action executes
}
public void OnActionExecuted(ActionExecutedContext context)
{
// do something after the action executes
}
}
}
The CustomActionFilter class implements the IActionFilter interface type, which offers two different methods:
- The OnActionExecuting method is triggered before the execution of the action.
- The OnActionExecuted method is executed after...