我有一个需要“主题”功能的网站。所以,我在“Themes”文件夹中创建了一堆视图。例如:
/Themes/Theme 1/Views/Content.cshtml
/Themes/Theme 1/Views/Menu.cshtml
/Themes/Theme 2/Views/Content.cshtml
/Themes/Theme 2/Views/Menu.cshtml
/Themes/Theme 3/Views/Content.cshtml
/Themes/Theme 3/Views/Menu.cshtml
我还创建了Content
和Menu
视图效果图,并将Path
字段分别设置为/Themes/Theme 1/Views/Content.cshtml
和/Themes/Theme 1/Views/Menu.cshtml
。
我需要的是在sitecore管道中的某处将“Theme 1”替换为“Theme 2”,以便我可以全局设置主题并动态加载与该主题相对应的视图(而不是创建多个“Menu”和“内容”视图渲染)。
有办法吗?
答案 0 :(得分:4)
所以,这就是我所做的:
创建一个类来处理RenderRendering
管道流程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Diagnostics;
using Sitecore.Caching;
using Sitecore.Mvc.Extensions;
using Sitecore.Mvc.Presentation;
using Sitecore.Sites;
namespace MyNamespace.Pipelines.Response.RenderRendering
{
public class ReplacePathProcessor : Sitecore.Mvc.Pipelines.Response.RenderRendering.RenderRenderingProcessor
{
public override void Process(Sitecore.Mvc.Pipelines.Response.RenderRendering.RenderRenderingArgs args)
{
var viewRenderer = args.Rendering.Renderer as ViewRenderer;
if (viewRenderer != null && viewRenderer.ViewPath.StartsWith("%Theme%"))
{
// Get the current site
var site = Sitecore.Context.Site;
// Get the master database
var database = Sitecore.Data.Database.GetDatabase("master");
// Get the root item
var rootItem = database.GetItem(site.RootPath);
// replace the path
viewRenderer.ViewPath = viewRenderer.ViewPath.Replace("%Theme%", rootItem["Theme"]);
}
}
}
}
添加配置包含
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<mvc.renderRendering>
<processor type="MyNamespace.Pipelines.Response.RenderRendering.ReplacePathProcessor, __Code">
<patch:attribute name="type">MyNamespace.Pipelines.Response.RenderRendering.ReplacePathProcessor, __Code</patch:attribute>
</processor>
</mvc.renderRendering>
</pipelines>
</sitecore>
</configuration>
将MvcSettings.RenderersViewFolder
更改为:
<setting name="Mvc.RenderersViewFolder" value="/Themes" />
更改我所有观看次数的ViewPath
字段以使用以下格式:
%Theme%/Views/{ViewName}.cshtml
希望能有所帮助。