ASP.NET Core MVC Interview Question #96

What is a result filter?

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

Quick Interview Answer

A result filter runs before and after execution of an MVC action result. It can inspect or modify result processing for supported result types.

Detailed Explanation

A result filter runs before and after execution of an MVC action result. It can inspect or modify result processing for supported result types.

Result filters are useful for MVC-specific concerns around result execution. They should not be used when ordinary middleware or a more explicit result implementation would be simpler and clearer.

Code Example

public sealed class HeaderResultFilter
    : IResultFilter
{
    public void OnResultExecuting(
        ResultExecutingContext context)
    {
        context.HttpContext.Response.Headers[
            "X-App-Version"] = "1";
    }

    public void OnResultExecuted(
        ResultExecutedContext context)
    {
    }
}