Skip to content

Instantly share code, notes, and snippets.

@deanebarker
Created September 27, 2017 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deanebarker/9abdbbb6ba8989e28b7cc02355ddefb0 to your computer and use it in GitHub Desktop.
Save deanebarker/9abdbbb6ba8989e28b7cc02355ddefb0 to your computer and use it in GitHub Desktop.
Episerver CMS: Route requests to alternate controllers and actions based on page properties.
using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using EPiServer.Web;
using System.Linq;
using System.Web;
namespace BlendInteractive.Business
{
/// <summary>
/// Checks a property of the rendering page for specified custom controller/action. This enables
/// "one-off" and "application-ish" pages to be an instance of an existing page type, but use a
/// different controller, action, or combination of the two.
/// </summary>
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class CustomRoutingResolver : IInitializableModule
{
/// <summary>
/// The name of the property which holds the custom controller value
/// </summary>
private const string controllerPropertyName = "CustomController";
/// <summary>
/// The name of the property which holds the custom action value
/// </summary>
private const string actionPropertyName = "CustomAction";
/// <summary>
/// Only execute for these page types
/// </summary>
private string[] allowedTypes = new[] { "StandardPage" };
public void Initialize(InitializationEngine context)
{
ServiceLocator.Current.GetInstance<TemplateResolver>().TemplateResolved += (object sender, TemplateResolverEventArgs e) =>
{
// This method gets called when trying to resolve a URL. In some cases, the context will be null.
if (HttpContext.Current == null)
{
return;
}
// If it's not a page, abandon.
if (!(e.ItemToRender is PageData))
{
return;
}
var page = (PageData)e.ItemToRender;
// If it's not an allowed type, abandon.
if(!allowedTypes.Contains(page.PageTypeName))
{
return;
}
// Handle custom controller assignment
if (page.GetPropertyValue(controllerPropertyName) != null)
{
var customControllerValue = page.GetPropertyValue(controllerPropertyName).Trim();
// Form the name of the desired controller
if (!customControllerValue.ToLower().EndsWith("controller"))
{
customControllerValue = customControllerValue + "Controller";
}
// Attempt to get that controller from the list of supported templates
var controller = e.SupportedTemplates.FirstOrDefault(t => t.Name.ToLower() == customControllerValue.ToLower());
if (controller != null)
{
e.SelectedTemplate = controller;
}
}
// Handle custom action assignment
if (page.GetPropertyValue(actionPropertyName) != null)
{
var customActionValue = page.GetPropertyValue(actionPropertyName).Trim();
HttpContext.Current.Request.RequestContext.RouteData.Values["action"] = customActionValue;
// Note that we have no way of verifying the action actually exists...
}
};
}
public void Preload(string[] parameters)
{
}
public void Uninitialize(InitializationEngine context)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment