Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed duplicate code and add note for obsolete method #6543

Merged
merged 2 commits into from
Oct 10, 2024
Merged

Conversation

sofietoft
Copy link
Contributor

Description

Removed duplicate code.
Added a note about the obsolete GetAll() method on the _domainService.

Type of suggestion

  • Typo/grammar fix
  • Updated outdated content
  • New content
  • Updates related to a new version
  • Other

Product & version (if relevant)

15.0.0

@sofietoft sofietoft added category/umbraco-cms review/developer Use this label if an internal developer review is required release/15.0.0 labels Oct 9, 2024
@bergmania
Copy link
Member

That entire example should be updated to something like

using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;

namespace RoutingDocs.ContentFinders;

public class My404ContentFinder : IContentLastChanceFinder
{
    private readonly IDomainService _domainService;
    private readonly IPublishedContentCache _publishedContentCache;

    public My404ContentFinder(
        IDomainService domainService,
        IPublishedContentCache publishedContentCache)
    {
        _domainService = domainService;
        _publishedContentCache = publishedContentCache;
    }

    public async Task<bool> TryFindContent(IPublishedRequestBuilder contentRequest)
    {
        // Find the root node with a matching domain to the incoming request
        var allDomains = (await _domainService.GetAllAsync(true)).ToList();
        var domain = allDomains
            .FirstOrDefault(f => f.DomainName == contentRequest.Uri.Authority
                                    || f.DomainName == $"https://{contentRequest.Uri.Authority}"
                                    || f.DomainName == $"http://{contentRequest.Uri.Authority}");

        var siteId = domain != null ? domain.RootContentId : allDomains.Any() ? allDomains.FirstOrDefault()?.RootContentId : null;


        var siteRoot = _publishedContentCache.GetById(false, siteId ?? -1);

        if (siteRoot is null)
        {
            return false;
        }

        // Assuming the 404 page is in the root of the language site with alias fourOhFourPageAlias
        var notFoundNode = siteRoot.Children()?.FirstOrDefault(f => f.ContentType.Alias == "fourOhFourPageAlias");

        if (notFoundNode is not null)
        {
            contentRequest.SetPublishedContent(notFoundNode);
        }

        // Return true or false depending on whether our custom 404 page was found
        return contentRequest.PublishedContent is not null;
    }
}

@sofietoft sofietoft merged commit 627e916 into main Oct 10, 2024
10 checks passed
@sofietoft sofietoft deleted the 15/getAll branch October 31, 2024 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category/umbraco-cms release/15.0.0 review/developer Use this label if an internal developer review is required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants