Eri*_*Yin 0 asp.net-mvc attributes action controller view
在视图中,例如,在"_Layout.cshtml"中
如何获取调用此视图的控制器/操作?
找到控制器/动作名称后,如何获取它具有的属性列表?或者测试它是否有属性?
谢谢.
@ViewContext.Controller将为您提供返回此视图的控制器实例.获得实例后,您将获得类型,一旦获得类型,您将进入Reflection以获取此类型所使用的属性.编写自定义HTML帮助程序来执行此作业可能是值得的:
public static class HtmlExtensions
{
public static bool IsDecoratedWithFoo(this HtmlHelper htmlHelper)
{
var controller = htmlHelper.ViewContext.Controller;
return controller
.GetType()
.GetCustomAttributes(typeof(FooAttribute), true)
.Any();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1505 次 |
| 最近记录: |