ASP.Net MVC操作筛选器:关于用法,OnActionExecuting和OnResultExecuting有什么区别 [英] ASP.Net MVC Action Filter : What is the difference between OnActionExecuting and OnResultExecuting regarding usage

查看:473
本文介绍了ASP.Net MVC操作筛选器:关于用法,OnActionExecuting和OnResultExecuting有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在寻找OnActionExecutingOnResultExecuting之间的比较.

basically i am looking for comparison between OnActionExecuting and OnResultExecuting.

什么时候应该使用OnActionExecuting,什么时候应该使用OnResultExecuting.

when we should work with OnActionExecuting and when should work with OnResultExecuting.

如果有人知道情况,则分享知识.谢谢

if anyone know the situation then share the knowledge. thanks

推荐答案

来自在ASP.NET MVC中进行过滤:

  • 动作过滤器.这些实现IActionFilter并包装action方法的执行. IActionFilter接口声明了两个方法:OnActionExecuting和OnActionExecuted. OnActionExecuting在操作方法之前运行. OnActionExecuted在操作方法之后运行,并且可以执行其他处理,例如向操作方法提供额外的数据,检查返回值或取消执行操作方法.

  • 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.

结果过滤器.它们实现IResultFilter并包装ActionResult对象的执行. IResultFilter声明两个方法:OnResultExecuting和OnResultExecuted. OnResultExecuting在执行ActionResult对象之前运行. OnResultExecuted在结果之后运行,并且可以对结果执行其他处理,例如修改HTTP响应. OutputCacheAttribute类是结果过滤器的一个示例.

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.

简而言之,这些是来自在不同时间执行的2种不同类型的过滤器的事件.

In short, these are events from 2 different types of filters that execute at different times.

IActionFilter.OnActionExecuting在action方法执行之前 执行. IResultFilter.OnResultExecuting之后执行该动作方法(即调用return View()),但在之前ActionResult执行.

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.

简而言之:OnActionExecuting可用于在业务逻辑运行之前进行干预. OnResultExecuting可用于在业务逻辑运行之后和显示逻辑运行之前进行干预.

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.

这篇关于ASP.Net MVC操作筛选器:关于用法,OnActionExecuting和OnResultExecuting有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆