问题
basically i am looking for comparison between OnActionExecuting
and OnResultExecuting
.
when we should work with OnActionExecuting
and when should work with OnResultExecuting
.
if anyone know the situation then share the knowledge. thanks
回答1:
From Filtering in ASP.NET MVC:
Action filters. These implement IActionFilter and wrap the action method execution. The IActionFilter interface declares two methods: OnActionExecuting and OnActionExecuted. OnActionExecuting runs before the action method. OnActionExecuted runs after the action method and can perform additional processing, such as providing extra data to the action method, inspecting the return value, or canceling execution of the action method.
Result filters. These implement IResultFilter and wrap execution of the ActionResult object. IResultFilter declares two methods: OnResultExecuting and OnResultExecuted. OnResultExecuting runs before the ActionResult object is executed. OnResultExecuted runs after the result and can perform additional processing of the result, such as modifying the HTTP response. The OutputCacheAttribute class is one example of a result filter.
In short, these are events from 2 different types of filters that execute at different times.
IActionFilter.OnActionExecuting
executes before the action method does. IResultFilter.OnResultExecuting
executes after the action method returns (i.e. calls return View()
), but before the ActionResult
executes.
In plain English: OnActionExecuting
can be used to intervene before the business logic runs. OnResultExecuting
can be used to intervene after the business logic runs and before the display logic runs.
来源:https://stackoverflow.com/questions/36099640/asp-net-mvc-action-filter-what-is-the-difference-between-onactionexecuting-and