Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Page not found content finder for Umbraco
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Routing;
public class PageNotFoundContentFinder : IContentFinder
public bool TryFindContent(PublishedContentRequest request)
// have we got any content?
if (request.PublishedContent == null)
// Get the root node for domain
var home = request.RoutingContext.UmbracoContext.ContentCache.GetByRoute(request.Domain.RootNodeId + "/");
// Try and find the 404 node
var notFoundNode = home.Children.Where(x => x.DocumentTypeAlias == "NotFoundPage").FirstOrDefault();
if (notFoundNode != null)
// Set Response Status to be HTTP 404
request.SetResponseStatus(404, "404 Page Not Found");
// Set the node to be the not found node
request.PublishedContent = notFoundNode;
// hopefully we will have content at this point
return request.PublishedContent != null;

This comment has been minimized.

Copy link

@patrickgomes1968 patrickgomes1968 commented Dec 20, 2017

I'm following this page of Warren's and was led here.
Could you please suggest the best place to save this file, and also, to which class this method needs to be added:
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { //On application starting event... //Add to the ContentFinder resolver collection our custom 404 Content Finder resolver ContentLastChanceFinderResolver.Current.SetFinder(new _404iLastChanceFinder()); }
Thanks in advance!

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