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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With