如何将描述附加到控制器(ASP.net MVC)中的每个 ActionMethod?

发布于 2024-09-06 22:32:37 字数 1051 浏览 2 评论 0原文

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

请参阅此处,了解我写给 为 ASP.NET MVC 创建一个扩展控制器类,解决了上述问题。

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

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

(1)创建我自己的属性,例如,描述并装饰每个控制器中的每个 ActionMethod,如下所示:

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

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

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

这是一个明智的做法吗?有更好的方法吗?

So recently, I came up with a way to dynamically build a nav-bar menu where the Controller (And its Index ActionMethod) would be listed in a horizontal bar at the top of each page (via Site.Master) but also based on the user's assigned roles. Each Controller listed would also list (in a vertical drop-down) the ActionMethods available to the user.

See here for the code I wrote to create an Extended Controller class for ASP.NET MVC which solved the above mentioned problem.

I realized that I also want to automatically create a default Index page for each Controller that lists all the ActionMethods available and a short 1 or 2 sentence description of each ActionMethod, but was pondering how to attach the description to the ActionMethod.

My assumption is that I need to do 3 things:

(1) Create my own Attribute, say, Description and decorate each ActionMethod in each Controller like so:

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

(2) Create a public ActionResult Index() method in my base class (ExtController) which will be used by every class inheriting ExtController to show a listing of the ActionMethods and their Descriptions available to that user.

(3) Create an Index.aspx View in Views/Shared that will build such a page.

Is this a sensible approach? Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

泛滥成性 2024-09-13 22:32:37

似乎是一个合理的方法。

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

Seems like a reasonable approach.

However there already is a DescriptionAttribute class in System.ComponentModel that you could probably leverage. There's some example code for reflecting on that property as well on the manual page.

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