Skip to content

Instantly share code, notes, and snippets.

@nlinker
Forked from leekelleher/MyApplication.cs
Created August 14, 2013 09:36
Show Gist options
  • Save nlinker/6229429 to your computer and use it in GitHub Desktop.
Save nlinker/6229429 to your computer and use it in GitHub Desktop.
using Umbraco.Core;
using Umbraco.Core.Services;
using Umbraco.Web.Mvc;
using Umbraco.Web.Routing;
namespace Our.Umbraco
{
public class MyApplication : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, ContentFinderForWhatever>();
base.ApplicationStarting(umbracoApplication, applicationContext);
}
}
// the example here is to have a 'virtual url'.
// this is required on a specific DocType, after @level=3
public class ContentFinderForWhatever : IContentFinder
{
public bool TryFindContent(PublishedContentRequest contentRequest)
{
if (contentRequest != null)
{
var path = contentRequest.Uri.GetAbsolutePathDecoded();
var parts = path.Split(new[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 2)
{
var level3 = string.Concat('/', string.Join("/", parts.Take(3)), '/');
var node = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute(level3);
if (node.DocumentTypeAlias == "Whatever")
contentRequest.PublishedContent = node;
}
}
return contentRequest.PublishedContent != null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment