How to attach a description to each ActionMethod in a Controller (ASP.net MVC)?
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 Action开发者_开发问答Methods 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?
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.
精彩评论