1

私は自分のビューエンジンを書いています。

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 カスタム属性

上記のSOの質問には、コントローラーの上に定義された属性を取得する方法があります。しかし、Action で定義された属性を取得する必要があります。

4

1 に答える 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
    }    
}
于 2012-09-26T10:10:06.917 に答える