[英]Skip OnActionExecuted execution
I made an ActionFilterAttribute that has the OnActionExecuted method implemented. 我制作了一个ActionFilterAttribute,它实现了OnActionExecuted方法。 That means, it runs after the Action method. 这意味着,它在Action方法之后运行。 But, in certain condition, I want the OnActionExecuted to not be executed. 但是,在某些情况下,我希望OnActionExecuted不被执行。
How do I, from the Action method, prevent the ActionFilter from being executed? 从Action方法中,我如何阻止ActionFilter被执行?
For now, I have made this: 现在,我已经做到了:
On the Action method: 关于Action方法:
RouteData.Values.Add("CancelActionFilter", true);
And on the ActionFilter.OnActionExecuted(): 并在ActionFilter.OnActionExecuted()上:
if (filterContext.RouteData.Values["CancelActionFilter"] != null)
{
return;
}
But I think that may exist a more elegant approach. 但我认为这可能存在一种更优雅的方法。
OnActionExecuted is called inside the InvokeActionMethodFilter method in the ControllerActionInvoker class. 在ControllerActionInvoker类的InvokeActionMethodFilter方法内调用OnActionExecuted。
Inside this method there's nothing to prevent the action of been executed. 在这种方法中,没有什么可以阻止执行的动作。 I think yours is a good solution. 我认为你的解决方案很好。
Code of ControllerActionInvoker class ControllerActionInvoker类的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.