@@ -141,78 +141,54 @@ To set your own 404 finder create an IContentLastChanceFinder and set it as the
141141A ` IContentLastChanceFinder ` will always return a 404 status code. This example creates a new implementation of the ` IContentLastChanceFinder ` and gets the 404 page for the current language of the request.
142142
143143``` csharp
144- using System .Linq ;
145- using System .Threading .Tasks ;
146- using Umbraco .Cms .Core .Models .PublishedContent ;
144+ using Umbraco .Cms .Core .PublishedCache ;
147145using Umbraco .Cms .Core .Routing ;
148146using Umbraco .Cms .Core .Services ;
149- using Umbraco .Cms .Core .Web ;
150147
151148namespace RoutingDocs .ContentFinders ;
152149
153150public class My404ContentFinder : IContentLastChanceFinder
154151{
155152 private readonly IDomainService _domainService ;
156- private readonly IUmbracoContextAccessor _umbracoContextAccessor ;
153+ private readonly IPublishedContentCache _publishedContentCache ;
157154
158- public My404ContentFinder (IDomainService domainService , IUmbracoContextAccessor umbracoContextAccessor )
155+ public My404ContentFinder (
156+ IDomainService domainService ,
157+ IPublishedContentCache publishedContentCache )
159158 {
160159 _domainService = domainService ;
161- _umbracoContextAccessor = umbracoContextAccessor ;
160+ _publishedContentCache = publishedContentCache ;
162161 }
163-
164- public Task <bool > TryFindContent (IPublishedRequestBuilder contentRequest )
162+
163+ public async Task <bool > TryFindContent (IPublishedRequestBuilder contentRequest )
165164 {
166165 // Find the root node with a matching domain to the incoming request
167- var allDomains = _domainService .GetAll (true ).ToList ();
168- var domain = allDomains ?
166+ var allDomains = ( await _domainService .GetAllAsync (true ) ).ToList ();
167+ var domain = allDomains
169168 .FirstOrDefault (f => f .DomainName == contentRequest .Uri .Authority
170169 || f .DomainName == $" https://{contentRequest .Uri .Authority }"
171170 || f .DomainName == $" http://{contentRequest .Uri .Authority }" );
172171
173172 var siteId = domain != null ? domain .RootContentId : allDomains .Any () ? allDomains .FirstOrDefault ()? .RootContentId : null ;
174173
175- if (! _umbracoContextAccessor .TryGetUmbracoContext (out var umbracoContext ))
176- {
177- return Task .FromResult (false );
178- }
179-
180- public Task < bool > TryFindContent (IPublishedRequestBuilder contentRequest )
181- {
182- // Find the root node with a matching domain to the incoming request
183- var allDomains = _domainService .GetAll (true ).ToList ();
184- var domain = allDomains ?
185- .FirstOrDefault (f => f .DomainName == contentRequest .Uri .Authority
186- || f .DomainName == $" https://{contentRequest .Uri .Authority }"
187- || f .DomainName == $" http://{contentRequest .Uri .Authority }" );
188-
189- var siteId = domain != null ? domain .RootContentId : allDomains .Any () ? allDomains .FirstOrDefault ()? .RootContentId : null ;
190174
191- if (! _umbracoContextAccessor .TryGetUmbracoContext (out var umbracoContext ))
192- {
193- return Task .FromResult (false );
194- }
195-
196- if (umbracoContext .Content == null )
197- return new Task <bool >(() => contentRequest .PublishedContent is not null );
198-
199- var siteRoot = umbracoContext .Content .GetById (false , siteId ?? - 1 );
175+ var siteRoot = _publishedContentCache .GetById (false , siteId ?? - 1 );
200176
201177 if (siteRoot is null )
202178 {
203- return Task . FromResult ( false ) ;
179+ return false ;
204180 }
205181
206182 // Assuming the 404 page is in the root of the language site with alias fourOhFourPageAlias
207- var notFoundNode = siteRoot .Children ? .FirstOrDefault (f => f .ContentType .Alias == " fourOhFourPageAlias" );
183+ var notFoundNode = siteRoot .Children () ? .FirstOrDefault (f => f .ContentType .Alias == " fourOhFourPageAlias" );
208184
209185 if (notFoundNode is not null )
210186 {
211187 contentRequest .SetPublishedContent (notFoundNode );
212188 }
213189
214190 // Return true or false depending on whether our custom 404 page was found
215- return Task . FromResult ( contentRequest .PublishedContent is not null ) ;
191+ return contentRequest .PublishedContent is not null ;
216192 }
217193}
218194```
0 commit comments