How to set properties in a custom WebViewPage?

白昼怎懂夜的黑 提交于 2020-01-13 14:54:29

问题


I have a very basic custom WebViewPage class in which I want to assign a value to a custom property. Here's my code:

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-->

From what I can tell the Execute method is not being called for some reason because I never hit any of the breakpoints I put in it. I have properly updated the Web.config to point to my custom WebViewPage and I can access the property from within the view, I just can't seem to set the property correctly.

I'd appreciate it if someone could point me in the right direction.


回答1:


Well, I don't know why Execute isn't being called, but InitializePage is, so I overrode it instead. This works:

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


来源:https://stackoverflow.com/questions/24943701/how-to-set-properties-in-a-custom-webviewpage

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