如何在自定义WebViewPage中设置属性?

时间:2014-07-24 20:50:10

标签: c# asp.net asp.net-mvc asp.net-mvc-5

我有一个非常基本的自定义WebViewPage类,我想在其中为自定义属性赋值。这是我的代码:

public abstract class WebViewPage :
    System.Web.Mvc.WebViewPage {
    public Employee UserPrincipal { get; set; }

    public override void Execute() {
        // Controller is also a custom class that contains the same property which is injected by
        // a custom ActionFilterAttribute. I've confirmed that the attribute is correctly updating
        // the property.
        Controller controller = (this.ViewContext.Controller as Controller);

        if (controller != null) {
            this.UserPrincipal = controller.UserPrincipal;
        }
    }
}

    <pages pageBaseType="X.Mvc.WebViewPage">
        <!--System.Web.Mvc.WebViewPage-->

据我所知,Execute方法由于某些原因没有被调用,因为我从未点击过我放入的任何断点。我已正确更新了Web.config以指向我的自定义WebViewPage,并且我可以在视图中访问该属性,我似乎无法正确设置该属性。

如果有人能指出我正确的方向,我会很感激。

1 个答案:

答案 0 :(得分:3)

好吧,我不知道为什么Execute没有被调用,但是InitializePage ,所以我改为覆盖它。这有效:

public abstract class WebViewPage :
    System.Web.Mvc.WebViewPage {
    public Employee UserPrincipal { get; set; }

    protected override void InitializePage() {
        base.InitializePage();

        Controller controller = (this.ViewContext.Controller as Controller);

        if (controller != null) {
            this.UserPrincipal = controller.UserPrincipal;
        }
    }
}