Sency
Sency

Reputation: 2878

Get Attribute value in ViewEngine ASP.NET MVC 3

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.

Upvotes: 1

Views: 840

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

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

Upvotes: 2

Related Questions