假设我写了一个自定义属性......
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)
{
}
}
然后我创建一个自定义的ViewEngine,并覆盖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);
}
我希望能够利用自定义属性将各种“标志”传递给自定义视图引擎。这有可能吗?
答案 0 :(得分:5)
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);
}