Created
December 16, 2014 18:07
-
-
Save tcmorris/4f23489db83e87e32706 to your computer and use it in GitHub Desktop.
Page not found content finder for Umbraco
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 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!