Skip to content

Instantly share code, notes, and snippets.

@joeriks
Last active December 17, 2015 15:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joeriks/5630458 to your computer and use it in GitHub Desktop.
Save joeriks/5630458 to your computer and use it in GitHub Desktop.
Catching all requests to URLs beneath a node if it has umbracoCatchAll=1, or at the same level if it's named * Example 1: add a document named * wherever and it will catch all "sibling" url calls (for example /products/* will catch /products/foo and /products/bar) Example 2: add a property umbracoCatchAll to a document and set it to 1 and it wil…
<?xml version="1.0" encoding="utf-8" ?>
<NotFoundHandlers>
<notFound assembly="umbraco" type="SearchForAlias" />
<notFound assembly="umbraco" type="SearchForTemplate"/>
<notFound assembly="umbraco" type="SearchForProfile"/>
<notFound assembly="CatchAllRouteHandler" namespace="Our" type="CatchAll"/>
<notFound assembly="umbraco" type="handle404"/>
</NotFoundHandlers>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using uComponents.Core;
using umbraco;
using umbraco.interfaces;
using uComponents.Core.uQueryExtensions;
using umbraco.presentation.nodeFactory;
namespace Our
{
/// <summary>
/// A handler for catching all requests to URLs beneath a node if it has umbracoCatchAll=1, or at the same level if it's named *
/// </summary>
public class CatchAllHandler : INotFoundHandler
{
private int redirectId;
public static string UrlParts(int part)
{
var currentNode = uQuery.GetCurrentNode();
var existingNodeUrl = (currentNode.Name == "*" ? currentNode.Parent : currentNode).NiceUrl;
if (existingNodeUrl.Contains("//"))
{
var startOfPath = existingNodeUrl.IndexOf("/", existingNodeUrl.IndexOf("//") + 2);
if (startOfPath != -1)
{
existingNodeUrl = existingNodeUrl.Substring(startOfPath);
}
}
var currentUrl = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
if (currentUrl.StartsWith(existingNodeUrl, StringComparison.CurrentCultureIgnoreCase))
{
var rest = (existingNodeUrl.Length == currentUrl.Length) ? "" : currentUrl.Substring(existingNodeUrl.Length + 1);
var urlDecoded = HttpContext.Current.Server.UrlDecode(rest);
var urlParts = urlDecoded.Split('/');
if (part > (urlParts.Count() - 1)) return null;
return urlParts[part];
}
return null;
}
public bool CacheUrl
{
get { return false; }
}
public Node getNodeByUrl(string url)
{
var urlParts = url.Split('/');
var currentPath = "";
var foundCatchAllNode = null as Node;
var foundNode = null as Node;
foreach (var urlPart in urlParts)
{
currentPath = currentPath + (currentPath != "" ? "/" : "") + urlPart;
var node = uQuery.GetNodeByUrl(currentPath);
if (node.Id == -1)
break;
if (node.GetProperty("umbracoCatchAll") != null && node.GetProperty("umbracoCatchAll").Value == "1") foundCatchAllNode = node;
foundNode = node;
}
if (foundNode != null && foundNode.Id != -1)
{
var catchAllChildNode = foundNode.GetChildNodeByName("*");
if (catchAllChildNode != null) foundCatchAllNode = catchAllChildNode;
}
return foundCatchAllNode;
}
public bool Execute(string url)
{
var node = getNodeByUrl(url);
if (node != null)
{
redirectId = node.Id;
return true;
}
return false;
}
public int redirectID
{
get { return redirectId; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment