Skip to content

Instantly share code, notes, and snippets.

@garpunkal
Last active January 21, 2020 13:15
Show Gist options
  • Save garpunkal/b747cdb7ba0cfbe05e0bd36ef8427b92 to your computer and use it in GitHub Desktop.
Save garpunkal/b747cdb7ba0cfbe05e0bd36ef8427b92 to your computer and use it in GitHub Desktop.
LastChanceContentFinder.cs
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.PublishedContentModels;
using Umbraco.Web.Routing;
namespace Web.ContentFinders
{
public class LastChanceContentFinder : IContentFinder
{
public bool TryFindContent(PublishedContentRequest contentRequest)
{
if (!contentRequest.Is404) return contentRequest.PublishedContent != null;
contentRequest.SetResponseStatus(404, "404 Page Not Found");
contentRequest.PublishedContent = GetFileNotFound(contentRequest, contentRequest.Uri.Segments[1].TrimEnd("/"));
return contentRequest.PublishedContent != null;
}
private static IPublishedContent GetFileNotFound(PublishedContentRequest contentRequest, string languageCode)
{
var helper = new UmbracoHelper(contentRequest.RoutingContext.UmbracoContext);
var homepage = helper.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == Homepage.ModelTypeAlias);
var fileNotFound = homepage?.FirstChild(x => x.DocumentTypeAlias == FileNotFound.ModelTypeAlias);
return fileNotFound;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment