ContentFinderByNotFoundHandlers works locally but not on server
Hi all,
For handeling the 404 not found pages of my application i created an ApplicationEventHandler. The code is as folllows:
public class ErrorPageHandler : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
LogHelper.Info<ErrorPageHandler>("application started");
ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, NotFoundContentFinder>();
base.ApplicationStarting(umbracoApplication, applicationContext);
}
internal class NotFoundContentFinder : IContentFinder
{
public bool TryFindContent(PublishedContentRequest contentRequest)
{
LogHelper.Info<ErrorPageHandler>($"has domain {contentRequest.HasDomain}");
if (contentRequest?.HasDomain == true)
{
var currentSiteRootNode = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(contentRequest.UmbracoDomain.RootContentId.Value);
LogHelper.Info<ErrorPageHandler>($"currentSiteNode {currentSiteRootNode}");
if (currentSiteRootNode != null)
{
var notFound = currentSiteRootNode.Descendant<NotFound>();
contentRequest.SetResponseStatus(404, "404 Page Not Found");
contentRequest.PublishedContent = notFound;
return contentRequest.PublishedContent != null;
}
}
return false;
}
}
}
This code works locally, and returns my custom NotFound models' publishedContent. However, when i push this to a server, i get the generic 404 page of IIS.
When I add some debugging strings to the code, I see that this event handler is indeed ran by umbraco on a 404, and that it can find my NotFound model. I can also see that the published content is not null on the server, so the logic of the above handler (should) be fine.
What could possibly be the reason that I see the standard IIS 404 page? Umbraco can find my published content and can confirm that it is not equal to null.
This sounds dumb but I usually remove the value from the error404 tag in umbracoSettings.config, although I have no proof it actually works. I suppose you could try this?
ContentFinderByNotFoundHandlers works locally but not on server
Hi all,
For handeling the 404 not found pages of my application i created an ApplicationEventHandler. The code is as folllows:
This code works locally, and returns my custom NotFound models' publishedContent. However, when i push this to a server, i get the generic 404 page of IIS.
When I add some debugging strings to the code, I see that this event handler is indeed ran by umbraco on a 404, and that it can find my NotFound model. I can also see that the published content is not null on the server, so the logic of the above handler (should) be fine.
What could possibly be the reason that I see the standard IIS 404 page? Umbraco can find my published content and can confirm that it is not equal to null.
Any help is appreciated.
Try the following:
Re-deploy and see if it works.
Alex,
Thank you for your reply! However, I already tried that..
Any other suggestions ? :)
This sounds dumb but I usually remove the value from the error404 tag in umbracoSettings.config, although I have no proof it actually works. I suppose you could try this?
Edit: I wrote a dodgy blog on this. What you've done is almost identical to what I do which almost always works for me. https://abrowndev.co.uk/blog/creating-a-custom-404-page/
Alex,
I fixed it! I added the following code web.config:
And it works now. It was a configuration fault. My bad!
Anyway, thanks for your help!
is working on a reply...