Skip to content

Instantly share code, notes, and snippets.

@herskinduk
Last active December 20, 2015 07:19
Show Gist options
  • Save herskinduk/6092642 to your computer and use it in GitHub Desktop.
Save herskinduk/6092642 to your computer and use it in GitHub Desktop.
Experimental Sitecore ChildActionRenderer - like ControllerRenderer, but using the HtmlHelper Action extension method for rendering. Standard MVC child action behaviour wrt TempData. Has dependencies on the Sitecore.MVC.Contrib project.
using Sitecore.Mvc.Common;
using Sitecore.Mvc.Presentation;
using Sitecore.Mvc.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Mvc.ExpressionUtil;
namespace Sitecore.Mvc.Contrib.Presentation.Renderer
{
public class ChildActionRenderer : Mvc.Presentation.ControllerRenderer
{
private readonly IPageContext _pageContext;
public ChildActionRenderer() : this(new PageContextWrapper(PageContext.Current))
{
}
public ChildActionRenderer(IPageContext pageContext)
{
_pageContext = pageContext;
}
public override void Render(System.IO.TextWriter writer)
{
string controllerName = this.ControllerName;
string actionName = this.ActionName;
if (!controllerName.IsWhiteSpaceOrNull() && !actionName.IsWhiteSpaceOrNull())
{
var value = RunChildAction(actionName, controllerName);
if (value == null)
{
return;
}
writer.Write(value);
}
}
private MvcHtmlString RunChildAction(string actionName, string controllerName)
{
var helper = GetHtmlHelper();
// Important note:
// The Action extension method requires that a route exists in the route table
// that maps the controller and action. This is standard child action behaviour.
return helper.Action(actionName, controllerName, _pageContext.RequestContext.RouteData.Values);
}
protected virtual HtmlHelper GetHtmlHelper()
{
ViewContext current = ContextService.Get().GetCurrent<ViewContext>();
return new HtmlHelper(current, new ViewDataContainer(current.ViewData));
}
}
}
using Sitecore.Mvc.Contrib.Presentation.Renderer;
using Sitecore.Mvc.Pipelines.Response.GetRenderer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sitecore.Mvc.Contrib.Pipelines.Response.GetRenderer
{
public class ChildAction : GetControllerRenderer
{
protected override Mvc.Presentation.Renderer GetRenderer(Mvc.Presentation.Rendering rendering, GetRendererArgs args)
{
Tuple<string, string> controllerAndAction = this.GetControllerAndAction(rendering, args);
if (controllerAndAction == null)
{
return null;
}
string str = controllerAndAction.Item1;
string str2 = controllerAndAction.Item2;
return new ChildActionRenderer { ControllerName = str, ActionName = str2 };
}
}
}
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<mvc.getRenderer>
<processor patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.GetRenderer.GetControllerRenderer, Sitecore.Mvc']" type="Sitecore.Mvc.Contrib.Pipelines.Response.GetRenderer.ChildAction, SitecoreMvcContrib" />
</mvc.getRenderer>
</pipelines>
</sitecore>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment