2012-09-26 34 views
1

我在寫我自己的視圖引擎。在ViewEngine中獲取屬性值ASP.NET MVC 3

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

上方,問題有如何獲得在控制器上面定義的屬性。但我需要獲取Action上定義的屬性。

回答

2
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 
    }  
}