Last active
December 19, 2015 13:59
-
-
Save leekelleher/5966488 to your computer and use it in GitHub Desktop.
Example of Umbraco 6.1's IContentFinder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
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
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