【问题标题】:How to attach a description to each ActionMethod in a Controller (ASP.net MVC)?如何将描述附加到控制器(ASP.net MVC)中的每个 ActionMethod?
【发布时间】:2010-06-21 17:25:31
【问题描述】:

所以最近,我想出了一种动态构建导航栏菜单的方法,其中控制器(及其索引操作方法)将列在每个页面顶部的水平栏中(通过 Site.Master),但也基于用户分配的角色。列出的每个控制器还将列出(在垂直下拉列表中)用户可用的 ActionMethods。

请参阅此处了解我写给 create an Extended Controller class for ASP.NET MVC 的代码,它解决了上述问题。

我意识到我还想为每个控制器自动创建一个默认索引页面,其中列出了所有可用的 ActionMethods 以及每个 ActionMethod 的简短 1 或 2 句描述,但正在思考如何将描述附加到 ActionMethod。

我的假设是我需要做 3 件事:

(1) 创建我自己的属性,比如描述,并像这样装饰每个控制器中的每个动作方法:

[Authorize(Roles="Admin")]
public class SomeController : ExtController
{
   [Description("This method adjusts all of your widgets making them better.")]
   public ActionResult TweakWidgets()
   {
      return View();
   }
}

(2) 在我的基类 (ExtController) 中创建一个 public ActionResult Index() 方法,每个继承 ExtController 的类都将使用该方法来显示可用的 ActionMethod 及其描述的列表用户。

(3) 在 Views/Shared 中创建一个 Index.aspx 视图来构建这样一个页面。

这是一种明智的做法吗?有没有更好的方法来做到这一点?

【问题讨论】:

    标签: asp.net-mvc attributes custom-attributes actionmethod


    【解决方案1】:

    似乎是一种合理的方法。

    但是 System.ComponentModel 中已经有一个 DescriptionAttribute 类,您可能可以利用它。手册页上还有一些用于反映该属性的示例代码。

    【讨论】:

    • 啊,谢谢指点。我讨厌重新发明轮子,并且不确定是否已经存在 Description 属性。
    • Intellisense 没有为我提取任何东西,但我没有引用 System.ComponentModel,所以这很有意义......谢谢!
    猜你喜欢
    • 2011-07-18
    • 2011-02-12
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多