Skip to content

Instantly share code, notes, and snippets.

@nlinker
Forked from joeriks/404handlers.config
Created August 14, 2013 12:41
Show Gist options
  • Save nlinker/6230688 to your computer and use it in GitHub Desktop.
Save nlinker/6230688 to your computer and use it in GitHub Desktop.
<?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