【问题标题】:Get Attribute value in ViewEngine ASP.NET MVC 3在 ViewEngine ASP.NET MVC 3 中获取属性值
【发布时间】:2012-09-26 09:36:26
【问题描述】:
我正在编写自己的视图引擎。
public class MyViewEngine : RazorViewEngine
{
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
// Here, how do I get attributes defined on top of the Action ?
}
}
ASP.NET MVC Custom Attributes within Custom View Engine
以上 SO 问题有如何获取在控制器之上定义的属性。但我需要在 Action 上定义属性。
【问题讨论】:
标签:
c#
asp.net-mvc
attributes
controller
【解决方案1】:
public class MyViewEngine : RazorViewEngine
{
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
var controllerType = controllerContext.Controller.GetType();
var actionDescriptor =
new ReflectedControllerDescriptor(controllerType)
.FindAction(
controllerContext,
controllerContext.RouteData.GetRequiredString("action")
);
var attributes = actionDescriptor.GetCustomAttributes(typeof(...), false);
// TODO: do something with the attributes that you retrieved
// from the current action
}
}