Skip to content

Instantly share code, notes, and snippets.

@garpunkal
Created January 21, 2020 13:24
Show Gist options
  • Save garpunkal/f2986be2e6c55dde80aba55fd625b9db to your computer and use it in GitHub Desktop.
Save garpunkal/f2986be2e6c55dde80aba55fd625b9db to your computer and use it in GitHub Desktop.
LastChanceContentFinder with global
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Routing;
namespace Business.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("/")) ??
GetGlobalFileNotFound(contentRequest);
return contentRequest.PublishedContent != null;
}
private static IPublishedContent GetFileNotFound(PublishedContentRequest contentRequest, string languageCode)
{
var helper = new UmbracoHelper(contentRequest.RoutingContext.UmbracoContext);
var global = helper
.TypedContentAtRoot()
.FirstOrDefault(x => x.DocumentTypeAlias == Shared.StaticValues.DocumentTypes.GlobalHomepage);
var language = global?
.FirstChild(x =>
x.DocumentTypeAlias == Shared.StaticValues.DocumentTypes.Homepage &&
string.Equals(x.Name, languageCode, System.StringComparison.OrdinalIgnoreCase));
var fileNotFound = language?
.FirstChild(x => x.DocumentTypeAlias == Shared.StaticValues.DocumentTypes.FileNotFound);
return fileNotFound;
}
private static IPublishedContent GetGlobalFileNotFound(PublishedContentRequest contentRequest)
{
var helper = new UmbracoHelper(contentRequest.RoutingContext.UmbracoContext);
var global = helper
.TypedContentAtRoot()
.FirstOrDefault(x => x.DocumentTypeAlias == Shared.StaticValues.DocumentTypes.GlobalHomepage);
var fileNotFound = global?
.FirstChild(x => x.DocumentTypeAlias == Shared.StaticValues.DocumentTypes.FileNotFound);
return fileNotFound;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment