From the legacy INotFoundHandler to IContentFinder.

by Damiaan Peeters 21. November 2013 09:05

If you are developing a custom “page not found” policy in Umbraco, then you know pretty good the “INotFoundHandler”.  

The old procedure is pretty straightforward. Create a new class which implements the INotFoundHandler interface and add an extra line in your “404handlers.config” file.  Done.

Did you know the INotFoundHandlers are replaced by IContentFinder?

Why bother

Are you really wondering why you would use the new interface “IContentFinder”, while the INotFoundHandler is still working.

First of all, the new IContentFinder  is documented.  How awesome is that? 

The INotFoundHandlers will become obsolete in the future.  So no reason to stay on legacy stuff. 

ContentFinders are very stable,  Umbraco v6 already uses ContentFinders to serve your content.  This is not instable alpha stuff you are looking at.

Notice the name change.  We go from “not found” to “content finder”.  That means that you can do a lot more than just handling not found requests.  That’s right!  You can now write your own blasting super geeky content finder which can serve any IPublishedContent (probably content from the tree). 

This new IContentFinder is a part of the request pipeline.  That means that IContentFinder classes can handle any request which is handled by Umbraco.  But this also means that you can insert your custom class before the normal Umbraco flow of searching elements by the “nice url”. 

A few examples:

  • If you don’t like the awesome 301 package UrlTracker by kipusoep, and you are considering building your own, you would want to use the new IContentFinder interface. 
  • If you are working with a multi site & multi language with a difficult 404 page setup, you just write your own 404 handler (and call the SetIs404() method on the PublishedContentRequest).
  • If you want to write your own rewriting rules against against some external database, you could use the IContentFinder
  • Serve content from a custom datasource.  I’ll try to discuss this briefly in another post.

How it works

First write your own ContentFinder.  You can do this by creating a new class, which implements the IContentFinder interface.  The only method you need is the TryFindContent.  Set the “PublishedContent” property to the node you want to returned to the user and return TRUE.  If your contentfinder did not found any content return FALSE so others can give there shot.

If you have a node you want to show as 404, put the node as the “publishedContent” property, call SetIs404  and return TRUE.

public class MyCustomContentFinder : IContentFinder 
{ 
    public bool TryFindContent(PublishedContentRequest contentRequest) 
    { 
        LogHelper.Debug<MyCustomContentFinder>("TryFindContent({0})", () => contentRequest.Uri.ToString());
if (contentRequest == null) { return contentRequest.PublishedContent != null; } var contentCache = UmbracoContext.Current.ContentCache; var foundContent = contentCache.GetById(1234); contentRequest.PublishedContent = foundContent; // contentRequest.SetIs404(); return contentRequest.PublishedContent != null; } }

To let Umbraco use the IContentFinder above, you will need to add the class to the ContentFinderResolver.  In this case we will insert it before the legacy “NotFoundHandlers”:

public class Custom404Launcher : ApplicationEventHandler
{
    protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        LogHelper.Info<Custom404Launcher>("Attaching MyCustomContentFinder as IContentFinder");
        ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, MyCustomContentFinder>();
    }
}

Watch (tail) the umbraco Log file with powershell

by Damiaan Peeters 6. November 2013 19:02

Situation

Sometimes you want to watch the umbraco Log file.  You doubelclick the file in explorer time after time.  Scroll down and look what was added on the bottom.  If you ar lazier (smarter?), then you would open WebMatrix or Visual studio leave the file open and just click yes when it reloads.

If you are still looking for the log files: go to /app_Data/Logs/UmbracoTraceLog.txt

Watching while it moves?

if you ever used linux then you are probably already missing the TAIL command for ages.  We have a solution: powershell to the rescue!  Make sure you have installed powershell version 3.

Then create a new powershell file (eg. mylogviewer.ps1) in the root of the website. Paste in the code below: 

gc App_Data\Logs\UmbracoTraceLog.txt -Tail 10 -Wait

When you run this powershell command, you will see the that powershell stays active and will update the screen as soon as new lines arrive in your LogFile.  If you don't want to create the file yourself, i have added it compressed below.

LogTail.zip (170.00 bytes)

Who.I.am

Certified Umbraco, .net and Azure developer, seo lover. Magician in my spare time.

Month List