asp.net mvc3 创建一个将始终执行的控制器方法

发布于 2024-12-12 06:16:43 字数 135 浏览 3 评论 0原文

我有一个基本控制器,其他所有控制器都继承自该控制器。我想在那里创建一个将在每个视图中执行的方法。现在我在基本控制器中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫言歌 2024-12-19 06:16:44

最简单的方法是使用 ActionFilterAttribute,在 MVC3 中,您可以全局或每个控制器注册属性。您不需要使用基本控制器。

您可以定义您的属性:

public class GlobalViewDataAttribute : ActionFilterAttribute { ... }

然后将其全局添加到 Global.asax.cs 中:

GlobalFilters.Filters.Add(new GlobalViewDataAttribute());

要在控制器中的每个操作上使用它,请使用:

[GlobalViewData]
public class MyController : Controller {}

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:

public class GlobalViewDataAttribute : ActionFilterAttribute { ... }

And then add it globally in your Global.asax.cs:

GlobalFilters.Filters.Add(new GlobalViewDataAttribute());

To use it on every action in a controller use:

[GlobalViewData]
public class MyController : Controller {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文