ASP.NET Core MVC Interview Question #93
What is an action filter?
Advanced Routing, Filters, Areas & Application Structure Mid-Level Intermediate
Quick Interview Answer
An action filter runs immediately before and/or after an MVC action method executes. It can inspect action arguments, ModelState, controller information, and the resulting action result.
Detailed Explanation
An action filter runs immediately before and/or after an MVC action method executes. It can inspect action arguments, ModelState, controller information, and the resulting action result.
Action filters are useful for focused cross-cutting MVC concerns such as logging or metrics. Business rules should normally remain in application or domain services rather than being hidden inside filters.
Code Example
public sealed class TimingFilter : IActionFilter
{
public void OnActionExecuting(
ActionExecutingContext context)
{
// Before action
}
public void OnActionExecuted(
ActionExecutedContext context)
{
// After action
}
}