【发布时间】:2011-01-14 16:35:17
【问题描述】:
我想在 MVC 2 应用程序中动态更改页面主题。 找到了几种解决方案,但我想用这个方法:在Global.asax中,更改当前页面主题:
protected void Application_PreRequestHandlerExecute(object sender, System.EventArgs e)
{
// cast the current handler to a page object
Page p = HttpContext.Current.Handler as Page;
if (p != null)
{
string strTheme = "Theme1";
if (Convert.ToString(HttpContext.Current.Session["THEME"]) != string.Empty)
strTheme = Convert.ToString(HttpContext.Current.Session["THEME"]);
p.StyleSheetTheme = strTheme;
}
}
但是这段代码总是在 "p" 中返回 null... 我也尝试过使用 HttpModule 中的事件 PreRequestHandlerExecute 和页面的 PreInit 事件的类似代码,但是代码
HttpContext.Current.Handler 作为页面
总是返回 null。
谁能帮帮我? 提前谢谢你。
【问题讨论】:
-
此技术不适用于 MVC。
-
您能简要解释一下原因吗,或者您有任何相关文档吗?谢谢。
-
据我了解,在控制器确定要呈现哪个视图之前,页面(它是一个 ViewPage,但它确实派生自 Page)不存在。假设它在 MVC 中完全设置在上下文中,这将在管道中进行得很晚。老实说,我没有详细研究它,但我只是按照我对 MVC 工作原理的理解。我不确定你希望它做什么,因为你不能使用任何它会影响的 ASP.NET 控件。
标签: asp.net asp.net-mvc asp.net-mvc-2 themes