Last active
January 21, 2020 13:15
-
-
Save garpunkal/b747cdb7ba0cfbe05e0bd36ef8427b92 to your computer and use it in GitHub Desktop.
LastChanceContentFinder.cs
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.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