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

标签 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 并且我可以从 View 中访问该属性,只是我似乎无法正确设置该属性。

如果有人能指出正确的方向,我将不胜感激。

最佳答案

嗯,我不知道为什么 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;
        }
    }
}

关于c# - 如何在自定义 WebViewPage 中设置属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24943701/

相关文章:

c# - 在 Visual Studio 中调用 javascript 中的类

c# - 如何访问 ASP.NET MVC 中 Html 元素的属性

c# - 在 ASP.NET Core 中连接子应用程序

jquery - ASP.NET MVC3 : ValidationType ModelClientValidationRule

c# - Epplus : how to display double quaotes in string concatenation formula

c# - 修剪字符串以添加特殊和非数字字符

asp.net - 升级到 TLS 1.2 后无法生成 Web 服务引用

c# - 动态添加元素到 jQuery Sortable

c# - EPPlus:如何遍历工作表的每个内容 block (单元格或合并区域)?

c# - list<list<int>> 使用 list[][] C# 赋值