Junior — Middle
Can you tell me about the different filters used in MVC architecture?
sobes.tech AI
Answer from AI
In MVC (Model-View-Controller) architecture in C#, filters are often used to inject additional logic at different stages of HTTP request processing. The main types of filters:
- Authorization Filters — check user access rights before executing a controller action.
- Action Filters — run before and after the action method, allowing logic to be executed around request processing.
- Result Filters — run before and after the result is generated (e.g., before rendering a view).
- Exception Filters — handle exceptions that occur during request processing.
Example of a simple Action Filter in C#:
public class LogActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
Console.WriteLine("Action execution started");
}
public override void OnActionExecuted(ActionExecutedContext context)
{
Console.WriteLine("Action completed");
}
}
Filters help centrally manage cross-cutting concerns such as logging, authorization, error handling, etc.