ASP.NET MVC Custom Attributes within Custom View Engine

故事扮演 提交于 2019-12-10 11:24:57

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/2976433/asp-net-mvc-custom-attributes-within-custom-view-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!