Skip to content

Instantly share code, notes, and snippets.

@tcmorris
Created December 16, 2014 18:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcmorris/4f23489db83e87e32706 to your computer and use it in GitHub Desktop.
Save tcmorris/4f23489db83e87e32706 to your computer and use it in GitHub Desktop.
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;
}
}
@patrickgomes1968
Copy link

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