在 SiteFinity 4 中,我们可以在用作页面模板的母版页的页面加载中设置主题名称吗?
最良好的祝愿,阿基
页面主题需要在asp.net的Page_PreInit事件中设置
不幸的是,母版页没有 Page_PreInit 事件,这是您需要设置主题的地方。
这是母版页本身的限制,而不是 Sitefinity。
但是,可以通过创建自定义路由处理程序来拦截请求并动态修改主题,从而在运行时设置主题。
首先创建一个自定义路由类:
public class CustomRouteHandler : PageRouteHandler
{
protected override void SetPageDirectives(Page handler, IPageData pageData)
{
base.SetPageDirectives(handler, pageData);
var url = HttpContext.Current.Request.RawUrl.ToLower();
var themeName = "AlternativeTheme";
if (url.StartsWith("/alternative"))
ThemeController.SetPageTheme(themeName, handler);
}
public static void RegisterType()
{
ObjectFactory.Container.RegisterType<PageRouteHandler, CustomRouteHandler>();
}
}
然后您需要在 Global.asax 文件中注册该路由:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Telerik.Sitefinity.Services.SystemManager.ApplicationStart += SystemManager_ApplicationStart;
}
void SystemManager_ApplicationStart(object sender, EventArgs e)
{
CustomRouteHandler.RegisterType();
}
}
现在每个请求都将通过该处理程序,您可以根据需要的任何自定义条件修改主题。
我计划整理一篇博客文章和视频,详细介绍如何完成此操作以及您可以使用它做什么,但这是其工作原理的基本概念。
我希望这是有帮助的!