简体   繁体   English

OnActionExecuted和OnResultExecuting之间的区别

[英]Difference between OnActionExecuted and OnResultExecuting

What is the difference between OnActionExecuted and OnResultExecuting? OnActionExecuted和OnResultExecuting有什么区别? Do they both get fired right after each other once the action has been processed or does something happen in between these two methods. 一旦处理完动作或在这两种方法之间发生了某些事情,它们是否会在彼此之后立即被解雇。 Like the initialisation of the View Engine, ... 就像View Engine的初始化一样,......

They have a different context. 他们有不同的背景。

OnActionExecuted has a ActionExecutedContext in which you can view the result produced by the action. OnActionExecuted有一个ActionExecutedContext ,您可以在其中查看该操作产生的结果。 You can also see whether the action encountered an Exception and see if the exception was handled. 您还可以查看操作是否遇到异常并查看是否已处理异常。

OnResultExecuting has a ResultExecutingContext . OnResultExecuting有一个ResultExecutingContext This method gets called just before the ActionResult instance is invoked. 在调用ActionResult实例之前调用此方法。 You can examine the result of the method and possibly cancel the execution of the result. 您可以检查方法的结果,并可能取消结果的执行。 This will usually result in a blank response with status code 200. (you can't do this in the OnActionExecuted method). 这通常会导致状态代码为200的空白响应。(您不能在OnActionExecuted方法中执行此操作)。

From ActionFilterAttribute.OnResultExecuting Method 来自ActionFilterAttribute.OnResultExecuting Method

Called by the ASP.NET MVC framework before the action result executes. 执行操作结果之前由ASP.NET MVC框架调用。

From ActionFilterAttribute.OnActionExecuted Method 来自ActionFilterAttribute.OnActionExecuted Method

Called by the ASP.NET MVC framework after the action method executes. 在action方法执行由ASP.NET MVC框架调用。

They get called after one another. 他们互相打电话。

There can be no initialisation, since the ActionResult you are executing may not be rendering a View - ActionResults are free to do whatever they choose: return JSON, return a File, issue a redirect, etc. 无法初始化,因为您正在执行的ActionResult可能无法呈现视图 - ActionResults可以自由地执行任何选择:返回JSON,返回文件,发出重定向等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM