我有一个非常基本的自定义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
,并且我可以在视图中访问该属性,我似乎无法正确设置该属性。
如果有人能指出我正确的方向,我会很感激。
答案 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;
}
}
}