SHARE
TWEET

Kentico 12 Custom MVC View Engine

a guest Jul 22nd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Web.Mvc;
  3. using CMS.SiteProvider;
  4.  
  5. namespace Website.Infrastructure.ViewEngines
  6. {
  7.     public class MultiSiteViewEngine : RazorViewEngine
  8.     {
  9.         private bool _locationsSet;
  10.  
  11.         private void SetViewLocations(string themeName)
  12.         {
  13.             if (_locationsSet) return;
  14.  
  15.             ViewLocationFormats = new[]
  16.             {
  17.                 $"~/Views/Themes/{themeName}/{{1}}/{{0}}.cshtml",
  18.                 $"~/Views/Themes/{themeName}/Shared/{{0}}.cshtml"
  19.             };
  20.             PartialViewLocationFormats = ViewLocationFormats;
  21.             MasterLocationFormats = ViewLocationFormats;
  22.  
  23.             AreaViewLocationFormats = new[]
  24.             {
  25.                 $"~/Areas/{{2}}/Views/Themes/{themeName}/{{1}}/{{0}}.cshtml",
  26.                 $"~/Areas/{{2}}/Views/Themes/{themeName}/Shared/{{0}}.cshtml"
  27.             };
  28.             AreaPartialViewLocationFormats = AreaViewLocationFormats;
  29.             AreaMasterLocationFormats = AreaViewLocationFormats;
  30.  
  31.             _locationsSet = true;
  32.         }
  33.        
  34.         public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName,
  35.             string masterName, bool useCache)
  36.         {
  37.             SetViewLocations(SiteContext.CurrentSiteName);
  38.  
  39.             // If it's a Kentico view - return an empty result to
  40.             // force MVC to drop through to Kentico's own ViewEngine
  41.             if (IsKenticoEmbeddedView(viewName))
  42.             {
  43.                 return new ViewEngineResult(ViewLocationFormats);
  44.             }
  45.  
  46.             return base.FindView(controllerContext, viewName, masterName, useCache);
  47.  
  48.         }
  49.  
  50.         public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName,
  51.             bool useCache)
  52.         {
  53.             SetViewLocations(SiteContext.CurrentSiteName);
  54.  
  55.             // If it's a Kentico view - return an empty result to
  56.             // force MVC to drop through to Kentico's own ViewEngine
  57.             if (IsKenticoEmbeddedView(partialViewName))
  58.             {
  59.                 return new ViewEngineResult(PartialViewLocationFormats);
  60.             }
  61.  
  62.             return base.FindPartialView(controllerContext, partialViewName, useCache);
  63.  
  64.         }
  65.         private static bool IsKenticoEmbeddedView(string viewName)
  66.         {
  67.             return (viewName.StartsWith("~/", StringComparison.Ordinal))
  68.                    && viewName.IndexOf("Kentico/", StringComparison.OrdinalIgnoreCase) >= 0;
  69.         }
  70.     }
  71.  
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top