Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为控制器和操作方法设置元数据?_C#_Asp.net Mvc_Reflection - Fatal编程技术网

C# 如何为控制器和操作方法设置元数据?

C# 如何为控制器和操作方法设置元数据?,c#,asp.net-mvc,reflection,C#,Asp.net Mvc,Reflection,我使用此代码显示所有控制器名称和操作方法: public ActionResult GetAllControllers() { var controllers = typeof (MvcApplication).Assembly.GetTypes().Where(typeof (IController).IsAssignableFrom); return View(controllers.ToList()); }

我使用此代码显示所有控制器名称和操作方法:

public ActionResult GetAllControllers()
        {
            var controllers = typeof (MvcApplication).Assembly.GetTypes().Where(typeof (IController).IsAssignableFrom);
            return View(controllers.ToList());
        }
视图:

现在我的问题是:有没有办法将控制器和操作方法的名称设置为显示结果?例如,类似这样的事情:

HomeController
     Index
     About
MainController
     Index
     Create
     Edit
     Delete
...
[SetName("MainPage")]
public class HomeController : Controller
{
    [SetName("ProductList")]
    public ActionResult Index()
    {
        //
    }
    // other action methods
}
因此,在这种情况下,显示控制器名称和操作方法的结果将是:

MainPage
   ProductList
... 

创建一个新的属性类,然后通过
Property
class

上的
GetCustomAttributes()
方法读取属性。我是一个非常熟练的C#用户,但可能你可以用过滤器来完成。你可以创建自己的属性并用它们标记你的方法。它们包含的数据可以通过您已经使用的反射来获得。
MainPage
   ProductList
...