Scott
Scott

Reputation: 2760

IActionFilter vs IResultFilter

Please explain the difference between IActionFilter and IResultFilter. I understand that OnActionExecuting happens before an action method executes, and that OnActionExecuted happens after an action method executes, and further, what it means for an action method to execute. What I don't understand, in the context of IResultFilter, is what it means for an action result to execute.

Upvotes: 7

Views: 6411

Answers (1)

shenku
shenku

Reputation: 12420

Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns.

Result filters (Or IResultFilters) contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.

Read Understanding Action Filters.

To clarify what a ViewResult does lets look at the ViewResultBase execution:

      viewEngineResult = this.FindView(context);
      this.View = viewEngineResult.View;

      TextWriter output = context.HttpContext.Response.Output;
      this.View.Render(new ViewContext(context, this.View, this.ViewData, this.TempData, output), output);

You will see it first finds the view, then renders the view to the Response output stream.

Upvotes: 7

Related Questions