Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Last active December 19, 2015 13:59
Show Gist options
  • Save leekelleher/5966488 to your computer and use it in GitHub Desktop.
Save leekelleher/5966488 to your computer and use it in GitHub Desktop.
Example of Umbraco 6.1's IContentFinder
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;
}
}
}
@leekelleher
Copy link
Author

For reference, original code was used from a forum post on Our Umbraco:
http://our.umbraco.org/forum/developers/extending-umbraco/41367-Umbraco-6-MVC-Custom-MVC-Route?p=1

@warrenbuckley
Copy link

Hey Lee,
Not sure what the purpose of this is and what it's attempting to do.

@leekelleher
Copy link
Author

It solved a specific problem that I had :-)

The example here is to create 'virtual URLs'. Say on a specific DocType, (called "Whatever", @Level = 3), you wanted to have deeper URLs for the same content.

e.g.

  • /gb/en/maps - this is an actual node
  • /gb/en/maps/europe - this is a virtual URL, that needs to use the parent 'maps' node.
  • /gb/en/maps/europe/uk - another virtual URL, that needs to use the 'maps' node (at @Level = 3)
    ... and so forth

Now I don't need to create fake/stub content nodes for all the URLs/pages.

@warrenbuckley
Copy link

Ah ok interesting use case. Will be starring this...

@stokedout
Copy link

This is great and just what I need. A good example I need it for is the blog aspect of a website.

In one area I have a central repository for blogs/posts so it's easy to manage

website.com/blog/black-labradors-are-great-pets

But to improve SEO we might have a landing page for dogs and be able to access the posts under a virtual url as suggested above.

website.com/dogs/black-labradors-are-great-pets

@Hendy
Copy link

Hendy commented Sep 3, 2014

Hi Lee,

Does the url influence the rendering of the page at all ? (I guess a hijack controller could look at the url and parse it again ?)

If data from the url needs to be used would it be best to follow [http://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/](Shannons post on custom MVC routes within the Umbraco pipeline) ?

Thanks,
Hendy

@ProNotion
Copy link

Hi Lee, I stumbled across this when trying to address a similar problem however I did hit the occassional null value on the node so amended the following:

if (node.DocumentTypeAlias == "Whatever")
to
if (node != null && node.DocumentTypeAlias == "Whatever")
Thanks, Simon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment