ASP.Net MVC Action Filter : What is the difference between OnActionExecuting and OnResultExecuting regarding usage

吃可爱长大的小学妹 提交于 2020-02-24 10:50:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!