[英]Call method every time a controller method is ending
I have a static class called NotificationManager and every time a controller method is called, I want to store the ModelErrors in the NotificationManager. 我有一个名为NotificationManager的静态类,每次调用控制器方法时,我都希望将ModelErrors存储在NotificationManager中。
However, inside the NotificationManager, I can't access the ModelState because it is not inside the actual controller. 但是,在NotificationManager中,我无法访问ModelState,因为它不在实际的控制器中。
Is there any way to automatically call a method once a controller method is finished, without having to write it in every single controller method? 有什么方法可以在控制器方法完成后自动调用方法,而不必在每个控制器方法中都编写它?
NOTE: I need to use the values in the view. 注意:我需要在视图中使用值。
You can create a global ActionFilter and override OnActionExecuted
. 您可以创建一个全局ActionFilter并覆盖OnActionExecuted
。
Alternatively, you could put a child action in the view. 或者,您可以在视图中放置一个子操作。
Override the OnActionExecuted in your controller. 覆盖控制器中的OnActionExecuted。 You still have ViewData[] 您仍然有ViewData []
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
//Do your stuff
base.OnActionExecuted(filterContext);
}
You can override methods in the Controller class to do this: 您可以在Controller类中重写方法来执行此操作:
http://msdn.microsoft.com/en-us/library/gg416513(v=vs.98).aspx http://msdn.microsoft.com/zh-CN/library/gg416513(v=vs.98).aspx
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onactionexecuted(v=vs.98).aspx http://msdn.microsoft.com/zh-CN/library/system.web.mvc.controller.onactionexecuted(v=vs.98).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.