Last active
December 10, 2015 18:58
-
-
Save kevinobee/4478418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace Mvc.Website | |
{ | |
public class MvcApplication : Sitecore.Web.Application | |
{ | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
//routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
routes.MapRoute( | |
"HelloWorld", // Route name | |
"hello/{action}/{id}", // URL with parameters | |
new | |
{ | |
controller = "HelloWorld", | |
scItemPath = "/sitecore/content/yoursite", | |
scPlaceholder = "main-content", | |
partialView = "true", | |
id = UrlParameter.Optional | |
}); | |
} | |
protected void Application_Start() | |
{ | |
RegisterRoutes(RouteTable.Routes); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Web.Mvc; | |
using Sitecore.Mvc.Presentation; | |
namespace Sitecore.Mvc.Contrib.Pipelines.MvcEvents | |
{ | |
public class InjectViewInPlaceholderFilter : Mvc.Pipelines.MvcEvents.ResultExecuting.ResultExecutingProcessor | |
{ | |
/// <summary> | |
/// Filter for injecting the ViewResult of a controller into a Sitecore placeholder. | |
/// | |
/// Use this configuration snippet to enable: | |
/// <mvc.resultExecuting> | |
/// <processor type="Sitecore.Mvc.Contrib.Pipelines.MvcEvents.InjectViewInPlaceholderFilter, Sitecore.Mvc.Contrib"/> | |
/// </mvc.resultExecuting> | |
/// | |
/// Will react to all controllers with the scItemPath and scPlaceholder set in the route data. E.g.: | |
/// routes.MapRoute( | |
/// "HelloWorld", // Route name | |
/// "hello/{action}/{id}", // URL with parameters | |
/// new { controller = "HelloWorld", scItemPath = "/sitecore/content/home", scPlaceholder = "main", id = UrlParameter.Optional } | |
/// ); | |
/// </summary> | |
/// <param name="args">Pipeline arguments with filter context</param> | |
public override void Process(Mvc.Pipelines.MvcEvents.ResultExecuting.ResultExecutingArgs args) | |
{ | |
Sitecore.Diagnostics.Log.Info("Process called on InjectViewInPlaceholderFilter", this); | |
var filterContext = args.Context; | |
var viewResult = filterContext.Result as ViewResult; | |
if (viewResult == null) return; | |
var placeholder = filterContext.RouteData.Values["scPlaceholder"] as string; | |
if (string.IsNullOrEmpty(placeholder) || PageContext.Current.Item == null) return; | |
var viewPath = (viewResult.ViewName != "") | |
? viewResult.ViewName | |
: filterContext.Controller.ControllerContext.RouteData.GetRequiredString("action"); | |
var viewEngineResult = GetView(viewResult, filterContext, viewPath); | |
viewResult.View = InjectViewInPlace(placeholder, filterContext.Controller.ControllerContext, | |
viewEngineResult.View, | |
viewResult.ViewData, viewResult.TempData); | |
} | |
private static ViewEngineResult GetView(ViewResult viewResult, ControllerContext filterContext, string viewPath) | |
{ | |
return IsPartialView(filterContext) | |
? viewResult.ViewEngineCollection.FindPartialView(filterContext.Controller.ControllerContext, viewPath) | |
: viewResult.ViewEngineCollection.FindView(filterContext.Controller.ControllerContext, viewPath, viewResult.MasterName); | |
} | |
private static bool IsPartialView(ControllerContext filterContext) | |
{ | |
bool usePartialView; | |
var partialView = filterContext.RouteData.Values["partialView"] as string; | |
if (string.IsNullOrWhiteSpace(partialView)) | |
{ | |
usePartialView = false; | |
} | |
else | |
{ | |
if (!bool.TryParse(partialView, out usePartialView)) | |
{ | |
usePartialView = false; | |
} | |
} | |
return usePartialView; | |
} | |
/// <summary> | |
/// Render a view and add it in placeholder on current page definition. | |
/// </summary> | |
/// <param name="placeholder"></param> | |
/// <param name="controllerContext"></param> | |
/// <param name="view"></param> | |
/// <param name="viewData"></param> | |
/// <param name="tempData"></param> | |
/// <returns></returns> | |
protected IView InjectViewInPlace(string placeholder, ControllerContext controllerContext, IView view, ViewDataDictionary viewData, TempDataDictionary tempData) | |
{ | |
Sitecore.Diagnostics.Log.Info("InjectViewInPlace called on InjectViewInPlaceholderFilter", this); | |
if (placeholder != null) | |
{ | |
var contentRendering = new ContentRendering | |
{ | |
Id = new Guid(), | |
UniqueId = new Guid(), | |
Placeholder = placeholder, | |
DeviceId = PageContext.Current.Device.Id | |
}; | |
using (var sw = new StringWriter()) | |
{ | |
var viewContext = new ViewContext(controllerContext, view, viewData, tempData, sw); | |
viewContext.View.Render(viewContext, sw); | |
contentRendering.Content = sw.ToString(); | |
} | |
var currentRenderings = PageContext.Current.PageDefinition.Renderings; | |
currentRenderings.Add(contentRendering); | |
} | |
return PageContext.Current.PageView; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<pipelines> | |
<!-- Mvc Events --> | |
<mvc.resultExecuting> | |
<processor type="Sitecore.Mvc.Contrib.Pipelines.MvcEvents.InjectViewInPlaceholderFilter, Sitecore.Mvc.Contrib"/> | |
</mvc.resultExecuting> | |
</pipelines> | |
</sitecore> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment