自定义视图引擎中的ASP.NET MVC自定义属性 [英] ASP.NET MVC Custom Attributes within Custom View Engine

查看:119
本文介绍了自定义视图引擎中的ASP.NET MVC自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我编写了一个自定义属性...

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

然后创建一个自定义ViewEngine,并覆盖FindView/FindPartialView ...

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?

推荐答案

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

这篇关于自定义视图引擎中的ASP.NET MVC自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆