Returning Umbracos 404 page correctly from a RenderController

I want to return 404 from a RenderController if certain criteria is met (or rather not met :))

I’ve come up with this code, that gets an IPublishedRequestBuilder, which then can be fed into the IContentLastChanceFinder to find the current 404 page, and then return that.

var request = await publishedRouter.CreateRequestAsync(umbracoContext.CleanedUmbracoUrl);
if (await contentLastChanceFinder.TryFindContent(request) && request.PublishedContent?.GetTemplateAlias() is {} view)
{
    HttpContext.Response.StatusCode = 404;
    return View(view, request.PublishedContent);
}

But this bypasses route hijacking for the 404 page. For now it’s probably not a problem, I’m just curious if there is a better way?

If I just return NotFound() it just gives an empty 404 page, instead of the one configured in Umbraco.

Have you perhaps already replaced the Concrete IContentLastChanceFinder ?

When adding a custom IContentLastChanceFinder to the pipeline any Error404Collection-settings in appSettings.json will be ignored.

I’m sure you’ve already seen the ContentFinderByConfigured404.cs

and the NotFoundHandlerHelper ?
:slight_smile:

I do have my own (from an internal package) IContentLastChanceFinder, and it works as expected. I just want to pipe the request through that one from my RenderController :slight_smile:

FYI: Custom 404 Error Page not working in umbraco 16 · Issue #19567 · umbraco/Umbraco-CMS · GitHub

incase it’s impacting here.