ASP.NET Core MVC Interview Question #90

What are MVC filters?

Advanced Routing, Filters, Areas & Application Structure Mid-Level Intermediate

Quick Interview Answer

MVC filters are components that run at defined stages around MVC action execution. They support cross-cutting concerns that are specific to MVC, such as authorization, resource handling, action execution, exception handling, and result processing.

Detailed Explanation

MVC filters are components that run at defined stages around MVC action execution. They support cross-cutting concerns that are specific to MVC, such as authorization, resource handling, action execution, exception handling, and result processing.

Filters can be applied globally, to a controller, or to individual actions. They should be used when the concern belongs to the MVC execution pipeline rather than the broader ASP.NET Core middleware pipeline.

Code Example

public class ProductsController : Controller
{
    [ServiceFilter(typeof(AuditActionFilter))]
    public IActionResult Edit(int id)
    {
        return View();
    }
}