Trying to write a 404 handler, following this guide and this thread, but it is not working correctly. I am not sure I am registering the event correctly?
then in the handler (code below) contentRequest.Is404 ALWAYS = false, even though the pages don't exist. When I try searching for example asdf.aspx, the contentRequest uri will show only /asdf (not the .aspx part). I end up seeing the default 404 page (set in umbracoSettings.config)
The handler gets fired, contentRequest.Is404 returns true, but I end up with the ugly 404. I would like it to fall through and return the default 404 page (since the handler isn't doing anything yet)
There is not much code in the handler yet, I am just trying to get it to fire and work correctly
using Umbraco.Core;
using Umbraco.Core.Services;
using Umbraco.Web;
using Umbraco.Web.Routing;
using umbraco;
using System.Linq;
namespace WHRX.Helpers {
public class NotFoundContentFinder : IContentFinder {
public bool TryFindContent(PublishedContentRequest contentRequest) {
if (contentRequest.Is404) {
var rootNode = uQuery.GetRootNode();
if (rootNode != null) {
// do something
}
}
return contentRequest.PublishedContent != null;
}
}
}
ok this is what I ended up doing that seems to work (adds 404 handler, defaults to default 404 page in config file if no other solution found). I added code to look up and display the default 404 page.
public bool TryFindContent(PublishedContentRequest contentRequest) {
Umbraco.Core.Models.IPublishedContent foundContent = null;
if (contentRequest.Is404) {
// add special code here to look up how to handle the 404
// if nothing found, try to find default error page
if (foundContent == null) {
// attempt to get default Error page
var defaultErrorPageList = umbraco.UmbracoSettings._umbracoSettings.SelectNodes("/settings/content/errors/error404");
if (defaultErrorPageList.Count > 0) {
int i404PageId = 0;
int.TryParse(defaultErrorPageList.Item(0).InnerText, out i404PageId);
foundContent = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(i404PageId);
}
// if you want, fall back to a hard coded number
foundContent = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(1062);
}
}
if (foundContent != null) contentRequest.PublishedContent = foundContent;
return contentRequest.PublishedContent != foundContent;
}
What I've done is i searched for the level 1 tree node, then search for the 2nd level tree where the document type is equal to "Templateerrorpage". In this scenario, every website must have 404 page in level 2.
public bool TryFindContent(PublishedContentRequest contentRequest)
{
//Check request is a 404
if (contentRequest.Is404)
{
//Get the home node
var home = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute("/");
//Get the 404 node
var notFoundNode = home.Children.Single(x => x.DocumentTypeAlias == "Template_error_page");
//Set Response Status to be HTTP 404
contentRequest.SetResponseStatus(404, "404 Page Not Found");
//Set the node to be the not found node
contentRequest.PublishedContent = notFoundNode;
}
return contentRequest.PublishedContent != null;
}
I had a similar issue on Umbraco 7.5.4. The problem in my case was that I didn't have a template set for the 404 page, so even though it was found, it could not be rendered.
404 handler using IContentFinder not working ?
Using Umbraco v6.2.1
Trying to write a 404 handler, following this guide and this thread, but it is not working correctly. I am not sure I am registering the event correctly?
When I have the following:
then in the handler (code below) contentRequest.Is404 ALWAYS = false, even though the pages don't exist. When I try searching for example asdf.aspx, the contentRequest uri will show only /asdf (not the .aspx part). I end up seeing the default 404 page (set in umbracoSettings.config)
When I change it to:
then the handler never gets fired at all. You see the default 404 page (from umbracoSettings.config)
And finally, when I have this:
The handler gets fired, contentRequest.Is404 returns true, but I end up with the ugly 404. I would like it to fall through and return the default 404 page (since the handler isn't doing anything yet)
There is not much code in the handler yet, I am just trying to get it to fire and work correctly
ok this is what I ended up doing that seems to work (adds 404 handler, defaults to default 404 page in config file if no other solution found). I added code to look up and display the default 404 page.
add the event handler (3rd version from above)
and the handler:
Hi Karen,
Thank you for your post, this helped me alot in Umbraco 7.1.9
I thought I'll share what i did as well. My code is based on your code above and the link you've given above
http://creativewebspecialist.co.uk/2013/08/07/the-new-way-to-do-a-404-umbraco-handler/
What I've done is i searched for the level 1 tree node, then search for the 2nd level tree where the document type is equal to "Templateerrorpage". In this scenario, every website must have 404 page in level 2.
Cheers
Shinsuke
Hi Shinsuke,
Perhaps you will also find this information useful http://24days.in/umbraco/2014/urlprovider-and-contentfinder/. It´s a blogpst written by Jeroen Breuer from the last year Umbraco advent calendar.
Hope this helps,
/Dennis
For anybody else that comes across this...
I had a similar issue on Umbraco 7.5.4. The problem in my case was that I didn't have a template set for the 404 page, so even though it was found, it could not be rendered.
Anyone had any issues with url that comes with special characters like
/fr-ca/à-propos-de/fdsafdsf
The Custome 404 Handler is not picking up with special characters in the url. Any idea?
The normal IIS error, i found out why.
within the HttpError tag in web.config, i need the existingResponse="PassThrough"
is working on a reply...