Is it possible to disable builtin "NotFound" resul...
# help-with-umbraco
l
I've spent too much time trying to hack the
IContentLastChanceFinder
, default renderer and whatnot, but it seems impossible to replace the builtin 404 placeholder page without resorting to a 302. Anyone managed to do this? I just want to display a static html file...
j
Oh, I have an example somewhere but I can't find it - the answer is Middleware, detect the 404 on the way out and respond with your own html file.
Alternatively, you may be able to override the template from the RCL which is at
~/umbraco/UmbracoWebsite/NotFound.cshtml
l
Aaahhh!! Latter seems a good idea. Will try later.
Cheers! (AFK for a while)
d
I use that one package from the marketplace to pick my 404 template from the Umbraco content
HotChillie notfound something something
j
DISCLAIMER - this is specifically for handling 404s with static files. If you want to serve an Umbraco page see the docs, or use the package mentionned above. Found my code, I remember now... Depending on what you want to serve 404s for, you may only need to do this in one place or two. If you need to serve custom 404s for non-umbraco paths - e.g. /somefile.txt, /somefile.html etc. the simplest way I've found is to use the built in UseStatusCodePagesWithReExecute middleware like this:
Copy code
csharp
//program.cs
//...
app.UseStatusCodePagesWithReExecute("/Errors/{0}.html");

app.UseUmbraco()
//...
Then just pop a static file at
~/wwwroot/Errors/404.html
. For Umbraco, the simplest thing to do is to override the view that Umbraco uses for it's default 404s. Add a new file at
~/umbraco/UmbracoWebsite/NotFound.cshtml
, this will overwrite the default one that ships with Umbraco (and will get called by Umbraco's built in middleware) You can combine the two approaches like so with a
NotFound.cshtml
that's a bit like this:
Copy code
csharp
@using Umbraco.Extensions
@using Microsoft.AspNetCore.Hosting
@inject IWebHostEnvironment host
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<cache>
@Html.Raw(await System.IO.File.ReadAllTextAsync(host.MapPathWebRoot("/Errors/404.html")))
</cache>
l
Cheers, @Jason. I'll go with the RCL override for now. Most important to keep real static for 500.
(Already had the status code use thing, but Umbraco is in it's way for 404)
Like a glove! (The razor works perfectly)
Also, I had statuscodepageswith_redirect_, so maybe that was what tripped me up.
But suspect Umbraco trumps the 404 status code handling so we need to do the RCL override for both.
4 Views