nan*_*ooh 13 asp.net-mvc action controller
如何获取Controller的所有操作列表?我搜索但找不到示例/答案.我看到一些推荐使用反射的例子,但我不知道如何.
这是我想要做的:
public List<string> ActionNames(string controllerName){
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 22
你没有告诉我们你为什么需要这个,但有一种可能性就是使用反射:
public List<string> ActionNames(string controllerName)
{
var types =
from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes()
where typeof(IController).IsAssignableFrom(t) &&
string.Equals(controllerName + "Controller", t.Name, StringComparison.OrdinalIgnoreCase)
select t;
var controllerType = types.FirstOrDefault();
if (controllerType == null)
{
return Enumerable.Empty<string>().ToList();
}
return new ReflectedControllerDescriptor(controllerType)
.GetCanonicalActions().Select(x => x.ActionName)
.ToList();
}
Run Code Online (Sandbox Code Playgroud)
显然,因为我们知道反射不是很快,所以如果你打算经常调用这个方法,你可以考虑通过缓存控制器列表来改进它,以避免每次都取出它,甚至为给定的输入参数记忆方法.
| 归档时间: |
|
| 查看次数: |
6531 次 |
| 最近记录: |