gpt4 book ai didi

asp.net-mvc-3 - HttpContext.Current 与 ActionExecutingContext

转载 作者:行者123 更新时间:2023-12-04 05:58:18 32 4
gpt4 key购买 nike

如果我正在编写这样的 ActionFilter

public override void OnActionExecuting(ActionExecutingContext filterContext)

在此方法中,我还可以通过 HttpContext.Current 访问当前上下文.我只是想知道这两种上下文有什么区别?

最佳答案

来自 article关于过滤器:

When you implement the OnActionExecuted method, you’ll end up receiving an instance of type ResultExecutedContext. Besides letting you get access to the currentControllerContext, you’ll also get properties for

  • getting the ActionResult (property Result) responsible for generating the response returned to the client;

  • seeing if the processing was cancelled (which happens when, for instance, you set the Result property of the ActionExecuting context from within the OnActionExecuting method). You can get this info from the Canceled property;

  • getting a reference to an exception that might have been generated during the processing (as you might expect, this is available through the Exception property);

  • indicating that you’ve handled an exception (property ExceptionHandled).

... The ResultExecutingContext is a little different from the ActionExecutingContext we’ve talked about. If you build one of these filters, then you can cancel further processing by setting the Cancel property to true from within the OnResultExecuting method. Notice that when this happens, your view won’t be called and you end up not generating the HTML that is generally sent back to the client (I’m assuming a simple case, where you have a view that generates HTML. As we’ll see, you can also have a view that generates JSON…).

关于asp.net-mvc-3 - HttpContext.Current 与 ActionExecutingContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9235210/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com