如何在自定义WebViewPage中设置属性? [英] How to set properties in a custom WebViewPage?
问题描述
我有一个非常基本的自定义WebViewPage
类,我想在其中为自定义属性分配一个值.这是我的代码:
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-->
据我所知,由于某种原因未调用Execute
方法,因为我从未遇到过放入它的任何断点.我已经正确地更新了Web.config,使其指向我的自定义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.
推荐答案
好吧,我不知道为什么没有调用Execute
,但是为什么InitializePage
是,所以我推翻了相反.这有效:
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;
}
}
}
这篇关于如何在自定义WebViewPage中设置属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!