Search code examples
asp.net-mvcviewengine

ASP.NET MVC Custom Attributes within Custom View Engine


Assuming I write a custom attribute...

public class SpecialActionFilterAttribute : System.Web.Mvc.ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
    }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // set some parameters here. 
    }
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
    }
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
    }
}

And then I create a custom ViewEngine, and override FindView/FindPartialView...

    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
    {
        // how can I get those parameters here? 

        return base.FindView(controllerContext, viewName, masterName, useCache);
    }

I'd like to be able to utilize the Custom Attribute to pass 'flags' of sorts to the custom view engine. is this at all possible?


Solution

  • public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
    {
        var controller = controllerContext.Controller;
    
        var controllerType = controller.GetType();
    
        //now we can use reflection
        var attributes = controllerType.GetAttributes();
    
        // how can I get those parameters here? 
    
        return base.FindView(controllerContext, viewName, masterName, useCache);
    }