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.

1 Answer 1

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
    }    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.