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