asp.net mvc3 创建一个将始终执行的控制器方法
我有一个基本控制器,其他所有控制器都继承自该控制器。我想在那里创建一个将在每个视图中执行的方法。现在我在基本控制器中有 void 方法,我只需在每个控制器中的每个操作中调用该方法。
这可能吗?
谢谢你,
H
I have a base controller from which every other controller inherits. I would like to create a method in there that will be executed in each view. Right now I have void method in Base controller that I just call in every action in every controller.
Is that even possible to do?
Thank you,
H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是使用 ActionFilterAttribute,在 MVC3 中,您可以全局或每个控制器注册属性。您不需要使用基本控制器。
您可以定义您的属性:
然后将其全局添加到 Global.asax.cs 中:
要在控制器中的每个操作上使用它,请使用:
The easiest way to do this is with an
ActionFilterAttribute
and in MVC3 you can register the attributes globally or per controller. You do not need to use a base controller.You can define your attribute:
And then add it globally in your Global.asax.cs:
To use it on every action in a controller use: