Created
April 18, 2025 05:49
-
-
Save AndyButland/cedcf3db4a1e4103654737a76c4c2ab6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Umbraco.Cms.Core.Models.PublishedContent; | |
using Umbraco.Cms.Core.Routing; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Core.Web; | |
namespace MyProject.Web.Customization; | |
public class SoftwarePageContentFinder : IContentFinder | |
{ | |
private readonly IDocumentUrlService _documentUrlService; | |
private readonly IUmbracoContextAccessor _umbracoContextAccessor; | |
public SoftwarePageContentFinder(IDocumentUrlService documentUrlService, IUmbracoContextAccessor umbracoContextAccessor) | |
{ | |
_documentUrlService = documentUrlService; | |
_umbracoContextAccessor = umbracoContextAccessor; | |
} | |
public Task<bool> TryFindContent(IPublishedRequestBuilder request) | |
{ | |
if (_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext? umbracoContext) is false) | |
{ | |
return Task.FromResult(false); | |
} | |
var path = request.Uri.GetAbsolutePathDecoded(); | |
// Only looking for software pages, found under the "/software" path. | |
if (path.StartsWith("/software") is false) | |
{ | |
return Task.FromResult(false); | |
} | |
// Look up the document key by route. | |
// This look-up will be using the registered segments. | |
Guid? documentKey = _documentUrlService.GetDocumentKeyByRoute(path, null, null, false); | |
if (documentKey.HasValue is false) | |
{ | |
return Task.FromResult(false); | |
} | |
// Make sure content not found when browsing via a default URL e.g. /software/business-software/hr-software. | |
// This is valid according to segments, as both "business-software" and "business" are registered as segments. | |
// But only /software/business/hr-software should be navigable. | |
var pathParts = path.Split('/'); | |
if (pathParts.Take(pathParts.Length - 1).Any(x => x.EndsWith("-software"))) | |
{ | |
// Need to explicitly set 404, otherwise the the default content finder by route will find it. | |
IPublishedContent? fileNotFoundContent = GetFileNotFoundContent(umbracoContext); | |
request.SetPublishedContent(fileNotFoundContent); | |
request.SetIs404(); | |
return Task.FromResult(true); | |
} | |
// Set the found contnent on the request. | |
IPublishedContent? content = umbracoContext.Content.GetById(documentKey.Value); | |
if (content is null) | |
{ | |
return Task.FromResult(false); | |
} | |
request.SetPublishedContent(content); | |
return Task.FromResult(true); | |
} | |
private static IPublishedContent? GetFileNotFoundContent(IUmbracoContext umbracoContext) | |
=> umbracoContext.Content.GetById(Guid.Parse("6e806c1f-9b61-437f-91d3-35650f38f560")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment