我有一个非常基本的自定义 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/