1

在我的 MVC3 C# 项目中,我一直在寻找在控制器代码中构建导航菜单并将其传递给母版页的方法。(我想在控制器中初始化菜单,以便我可以进行一些授权和角色检查等......)

到目前为止,我在这里找到了几个答案,似乎都需要覆盖 BaseController 的 OnActionExecuted 方法(所有其他控制器都从该方法扩展)

示例 1(参见接受的答案):如何使用 ASP.NET MVC 中的基本控制器创建强类型母版页

示例 2:“安全意识”操作链接?

但是为什么不把相同的代码放在基本控制器的构造函数中呢?

4

1 回答 1

1

If all your controllers inherit from the same base controller, there is no reason why you could not do it this way.

The advantage of an ActionFilterAttribute is that it allows you to insert your logic on any controller by simply decorating it with the attribute, allowing you the freedom of inheriting from any base class you choose.

于 2012-11-30T03:36:52.070 回答