IContentFinder
Information about creating your own content finders
To create a custom content finder, with custom logic to find an Umbraco document based on a request, implement the IContentFinder interface:
public interface IContentFinder
{
Task<bool> TryFindContent(IPublishedRequestBuilder contentRequest);
}and use either an Umbraco builder extension, or a composer to add it to it to the ContentFindersCollection.
Umbraco runs all content finders in the collection 'in order', until one of the IContentFinders returns true. Once this occurs, the request is then handled by that finder, and no further IContentFinders are executed. Therefore the order in which ContentFinders are added to the ContentFinderCollection is important.
The ContentFinder can set the PublishedContent item for the request, or template or even execute a redirect.
Example
This IContentFinders will find a document with id 1234, when the Url begins with /woot.
public class MyContentFinder : IContentFinder
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
public MyContentFinder(IUmbracoContextAccessor umbracoContextAccessor)
{
_umbracoContextAccessor = umbracoContextAccessor;
}
public Task<bool> TryFindContent(IPublishedRequestBuilder contentRequest)
{
var path = contentRequest.Uri.GetAbsolutePathDecoded();
if (path.StartsWith("/woot") is false)
{
return Task.FromResult(false); // Not found
}
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
{
return Task.FromResult(false);
}
// Have we got a node with ID 1234
var content = umbracoContext.Content.GetById(1234);
if (content is null)
{
// If not found, let another IContentFinder in the collection try.
return Task.FromResult(false);
}
// If content is found, then render that node
contentRequest.SetPublishedContent(content);
return Task.FromResult(true);
}
}Adding and removing IContentFinders
You can use either an extension on the Umbraco builder or a composer to access the ContentFinderCollection and add or remove specific ContentFinders.
Umbraco builder extension
First create the extension method:
Then invoke in the Program.cs file:
Composer
NotFoundHandlers
To set your own 404 finder create an IContentLastChanceFinder and set it as the ContentLastChanceFinder. (perhaps you have a multilingual site and need to find the appropriate 404 page in the correct language).
A 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.
You can configure Umbraco to use your own implementation in the Program.cs file:
When adding a custom IContentLastChanceFinder to the pipeline any Error404Collection-settings in appSettings.json will be ignored.
Last updated