Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 504
Scott Avatar asked Mar 05 '14 02:03

Scott


People also ask

What is difference between IActionFilter and ActionFilterAttribute?

ActionFilterAttribute is the basis for filtering actions and results, since is a implementation of IActionFilter, IResultFilter and inherit from FilterAttribute. Your MySecondFilterAttribute implementation leads to ActionFilterAttribute without IResultFilter capabilities (OnResultExecuting and OnResultExecuted).

What is difference between middleware and action filter?

Middleware has access to HttpContext but Filter has access to wider MVC Context which helps us to access routing data and model binding information. Filters are only part of MVC Middleware but middlewares are part of every request pipeline.

What is difference between middleware and filters in .NET Core?

Middlewares run on every request, regardless of which controller or action is called. Filters have full access to the MVC context , meaning that we have access to the: routing data, current controller, ModelState, etc. For example, we could create a filter that validates the input model.


1 Answers

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.

like image 114
shenku Avatar answered Nov 15 '22 07:11

shenku