Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@leekelleher leekelleher commented Jul 10, 2013

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

This comment has been minimized.

Copy link

@warrenbuckley warrenbuckley commented Jul 10, 2013

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

@leekelleher

This comment has been minimized.

Copy link
Owner Author

@leekelleher leekelleher commented Jul 10, 2013

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

This comment has been minimized.

Copy link

@warrenbuckley warrenbuckley commented Jul 10, 2013

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

@stokedout

This comment has been minimized.

Copy link

@stokedout stokedout commented Apr 25, 2014

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

This comment has been minimized.

Copy link

@Hendy 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

This comment has been minimized.

Copy link

@ProNotion ProNotion commented Jan 26, 2015

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
You can’t perform that action at this time.