1

I'm writting my own view engine.

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

Above SO Question has how to get attributes defined on top of the Controller. But I need to get attributes defined on Action.

Community
  • 1
  • 1
Sency
  • 2,818
  • 8
  • 42
  • 59

1 Answers1

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
    }    
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928