Created
July 29, 2013 06:30
-
-
Save herskinduk/6102481 to your computer and use it in GitHub Desktop.
Experimental Sitecore ControllerRunner with childaction behaviour. Based on the AreaControllerRunner from the Sitecore.MVC.Contrib project.
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 Sitecore.Mvc.Controllers; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Sitecore.Mvc.Presentation; | |
using Sitecore.Mvc.Common; | |
namespace Sitecore.Mvc.Contrib.Controllers | |
{ | |
public class AreaControllerRunner : ControllerRunner, IControllerRunner | |
{ | |
private readonly IPageContext _pageContext; | |
public AreaControllerRunner(IPageContext pageContext, AreaRouteData areaRouteData) | |
: base(areaRouteData.Controller, areaRouteData.Action) | |
{ | |
_pageContext = pageContext; | |
Area = areaRouteData.Area; | |
} | |
public AreaControllerRunner(AreaRouteData areaRouteData) | |
: this(new PageContextWrapper(PageContext.Current), areaRouteData) | |
{ | |
} | |
public string Area { get; set; } | |
protected override void ExecuteController(Controller controller) | |
{ | |
RequestContext requestContext = _pageContext.RequestContext; | |
object controllerValue = requestContext.RouteData.Values["controller"]; | |
object actionValue = requestContext.RouteData.Values["action"]; | |
object areaValue = requestContext.RouteData.DataTokens["area"]; | |
object parentViewContextValue = requestContext.RouteData.DataTokens["ParentActionViewContext"]; | |
try | |
{ | |
requestContext.RouteData.Values["controller"] = ActualControllerName; | |
requestContext.RouteData.Values["action"] = ActionName; | |
requestContext.RouteData.DataTokens["area"] = Area; | |
requestContext.RouteData.DataTokens["ParentActionViewContext"] = GetHtmlHelper().ViewContext; | |
// TODO: Investigate if routeValues will needs to be merged in. | |
// ChildActionValueProvider is used when you issue a child request using Html.Action and/or | |
// Html.RenderAction methods, so that your explicit parameter values in Html.Action and/or | |
// Html.RenderAction will always take precedence over other value providers. | |
//if (dictionary != null) | |
//{ | |
// routeValues[ChildActionValueProvider.ChildActionValuesKey] = new DictionaryValueProvider<object>(dictionary, CultureInfo.InvariantCulture); | |
//} | |
((IController)controller).Execute(_pageContext.RequestContext); | |
} | |
finally | |
{ | |
requestContext.RouteData.Values["controller"] = controllerValue; | |
requestContext.RouteData.Values["action"] = actionValue; | |
requestContext.RouteData.DataTokens["area"] = areaValue; | |
// TODO: Investigate if parentViewContextValue is always null. Test with nested ControllerRenderers. | |
if (parentViewContextValue == null) | |
requestContext.RouteData.DataTokens.Remove("ParentActionViewContext"); | |
else | |
requestContext.RouteData.DataTokens["ParentActionViewContext"] = parentViewContextValue; | |
} | |
} | |
protected virtual HtmlHelper GetHtmlHelper() | |
{ | |
ViewContext current = ContextService.Get().GetCurrent<ViewContext>(); | |
return new HtmlHelper(current, new ViewDataContainer(current.ViewData)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment