Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Web.Mvc;
- using CMS.SiteProvider;
- namespace Website.Infrastructure.ViewEngines
- {
- public class MultiSiteViewEngine : RazorViewEngine
- {
- private bool _locationsSet;
- private void SetViewLocations(string themeName)
- {
- if (_locationsSet) return;
- ViewLocationFormats = new[]
- {
- $"~/Views/Themes/{themeName}/{{1}}/{{0}}.cshtml",
- $"~/Views/Themes/{themeName}/Shared/{{0}}.cshtml"
- };
- PartialViewLocationFormats = ViewLocationFormats;
- MasterLocationFormats = ViewLocationFormats;
- AreaViewLocationFormats = new[]
- {
- $"~/Areas/{{2}}/Views/Themes/{themeName}/{{1}}/{{0}}.cshtml",
- $"~/Areas/{{2}}/Views/Themes/{themeName}/Shared/{{0}}.cshtml"
- };
- AreaPartialViewLocationFormats = AreaViewLocationFormats;
- AreaMasterLocationFormats = AreaViewLocationFormats;
- _locationsSet = true;
- }
- public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName,
- string masterName, bool useCache)
- {
- SetViewLocations(SiteContext.CurrentSiteName);
- // If it's a Kentico view - return an empty result to
- // force MVC to drop through to Kentico's own ViewEngine
- if (IsKenticoEmbeddedView(viewName))
- {
- return new ViewEngineResult(ViewLocationFormats);
- }
- return base.FindView(controllerContext, viewName, masterName, useCache);
- }
- public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName,
- bool useCache)
- {
- SetViewLocations(SiteContext.CurrentSiteName);
- // If it's a Kentico view - return an empty result to
- // force MVC to drop through to Kentico's own ViewEngine
- if (IsKenticoEmbeddedView(partialViewName))
- {
- return new ViewEngineResult(PartialViewLocationFormats);
- }
- return base.FindPartialView(controllerContext, partialViewName, useCache);
- }
- private static bool IsKenticoEmbeddedView(string viewName)
- {
- return (viewName.StartsWith("~/", StringComparison.Ordinal))
- && viewName.IndexOf("Kentico/", StringComparison.OrdinalIgnoreCase) >= 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement