Last active
August 29, 2015 14:02
-
-
Save asmagin/1d1a191a494764d063a9 to your computer and use it in GitHub Desktop.
MVC areas in Sitecore
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
namespace Examples.Common.Sc.Pipelines | |
{ | |
using System; | |
using Sitecore.Data; | |
using Sitecore.Mvc.Pipelines.Response.GetRenderer; | |
using Sitecore.Mvc.Presentation; | |
public class AreaController : GetRendererProcessor | |
{ | |
public override void Process(GetRendererArgs args) | |
{ | |
if (args.Result != null) | |
{ | |
return; | |
} | |
var renderingTemplate = args.RenderingTemplate; | |
if (renderingTemplate == null) | |
{ | |
return; | |
} | |
if (!renderingTemplate.DescendsFromOrEquals(new ID("{2A3E91A0-7987-44B5-AB34-35C2D9DE83B9}"))) | |
{ | |
return; | |
} | |
args.Result = this.GetRenderer(args.Rendering, args); | |
} | |
protected virtual Renderer GetRenderer(Rendering rendering, GetRendererArgs args) | |
{ | |
var action = rendering.RenderingItem.InnerItem.Fields["controller action"].GetValue(true); | |
var controller = rendering.RenderingItem.InnerItem.Fields["controller"].GetValue(true); | |
var area = rendering.RenderingItem.InnerItem.Fields["area"].GetValue(true); | |
var namespaces = rendering.RenderingItem.InnerItem.Fields["namespaces"].GetValue(true); | |
if (string.IsNullOrEmpty(namespaces)) | |
{ | |
namespaces = string.Empty; | |
} | |
return new AreaControllerRenderer | |
{ | |
Action = action, | |
Controller = controller, | |
Area = area, | |
Namespaces = namespaces.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) | |
}; | |
} | |
} | |
} |
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
namespace Examples.Common.Sc.Pipelines | |
{ | |
using System; | |
using System.IO; | |
using Sitecore.Mvc.Presentation; | |
using Sitecore.StringExtensions; | |
public class AreaControllerRenderer : Renderer | |
{ | |
public override string CacheKey | |
{ | |
get | |
{ | |
return "areacontroller::" + this.Controller + "#" + this.Action + "#" + this.Area; | |
} | |
} | |
public string Action { get; set; } | |
public string Area { get; set; } | |
public string Controller { get; set; } | |
public string[] Namespaces { get; set; } | |
public override void Render(TextWriter writer) | |
{ | |
var controllerRunner = new AreaControllerRunner(this.Controller, this.Action, this.Area, this.Namespaces); | |
var value = controllerRunner.Execute(); | |
if (string.IsNullOrEmpty(value)) | |
{ | |
return; | |
} | |
writer.Write(value); | |
} | |
public override string ToString() | |
{ | |
return "Controller: {0}. Action: {1}. Area {2}".FormatWith(new object[] { this.Controller, this.Action, this.Area }); | |
} | |
} | |
} |
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
namespace Examples.Common.Sc.Pipelines | |
{ | |
using System.Web.Mvc; | |
using Sitecore.Mvc.Controllers; | |
using Sitecore.Mvc.Presentation; | |
public class AreaControllerRunner : ControllerRunner | |
{ | |
public AreaControllerRunner(string controllerName, string actionName, string area, string[] namespaces) | |
: base(controllerName, actionName) | |
{ | |
this.Area = area; | |
Namespaces = namespaces; | |
} | |
public string Area { get; set; } | |
public string[] Namespaces { get; set; } | |
public override string Execute() | |
{ | |
var requestContext = PageContext.Current.RequestContext; | |
if (this.Namespaces != null && this.Namespaces.Length > 0) | |
{ | |
if (!requestContext.RouteData.DataTokens.ContainsKey("Namespaces")) | |
{ | |
requestContext.RouteData.DataTokens.Add("Namespaces", this.Namespaces); | |
} | |
else | |
{ | |
requestContext.RouteData.DataTokens["Namespaces"] = this.Namespaces; | |
} | |
} | |
return base.Execute(); | |
} | |
protected override void ExecuteController(Controller controller) | |
{ | |
var requestContext = PageContext.Current.RequestContext; | |
var value = requestContext.RouteData.Values["controller"]; | |
var value2 = requestContext.RouteData.Values["action"]; | |
var value3 = requestContext.RouteData.DataTokens["area"]; | |
try | |
{ | |
requestContext.RouteData.Values["controller"] = this.ActualControllerName; | |
requestContext.RouteData.Values["action"] = this.ActionName; | |
requestContext.RouteData.DataTokens["area"] = this.Area; | |
((IController)controller).Execute(PageContext.Current.RequestContext); | |
} | |
finally | |
{ | |
requestContext.RouteData.Values["controller"] = value; | |
requestContext.RouteData.Values["action"] = value2; | |
requestContext.RouteData.DataTokens["area"] = value3; | |
} | |
} | |
} | |
} |
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> | |
<mvc.getRenderer> | |
<processor patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.GetRenderer.GetViewRenderer, Sitecore.Mvc']" | |
type="Examples.Common.Sc.Pipelines.AreaController, Examples.Common.Sc" /> | |
</mvc.getRenderer> | |
</sitecore> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment