![](/img/trans.png)
[英]Having an HttpContext how can I get the "MethodInfo" of the action that is going to be executed?
[英]How can I get MVC action information from the action's MethodInfo?
在我的應用程序初始化時,我想創建一個其方法具有特定CustomAttribute的任何MVC操作的注冊表。 我希望該注冊表跟蹤MVC區域,控制器和操作。 我可能需要添加屬性的人員來指定此信息,但看來我應該能夠基於MethodInfo來找出此信息:基本上與調用ActionLink方法時發生的情況相反。 我怎樣才能做到這一點?
在程序集中搜索每個Controller
,然后在所有方法中搜索以找到具有特定屬性的方法。
// current assembly -> all types that have basetype controller -> grab methods
foreach(var type in System.Reflection.Assembly.GetCallingAssembly().GetTypes().Where(t=>
typeof(Controller).IsAssignableFrom(t))))
{
foreach(var methodInfo in type.GetMethods())
{
if (methodInfo.GetCustomAttributes(typeof(MyAttribute), false).Count() > 0)
{
var action = methodInfo.Name;
var controller = type.Name;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.