Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • andrew shearer 510 posts 659 karma points
    Oct 26, 2021 @ 01:44
    andrew shearer
    0

    Umbraco 9: custom 404 for missing static files

    Hello

    I am using a content finder (IContentLastChanceFinder) just fine for serving out a custom page for “Not found”/404 content requests. However, I am struggling to work out how to use a custom 404 page for static files (missing images etc).

    I see that in the Umbraco core source code that the method RegisterDefaultRequiredMiddleware() https://github.com/umbraco/Umbraco-CMS/blob/v9/contrib/src/Umbraco.Web.Common/ApplicationBuilder/UmbracoApplicationBuilder.cs Umbraco registers “UseStatusCodePages” and “UseStaticFiles”, so to compliment that I thought a technique from https://bauchart.be/2017/10/01/custom-404-pages-in-asp-net-core-for-routes-and-static-files/ where my middleware replaces a 404 with my custom path (older page I know but the approach still sounds ok):

        //Perform middleware for custom 404 page
    app.Use(async (context, next) => {
        await next();
        if(context.Response.StatusCode == 404)
        {
            context.Request.Path = "/404";
            await next();
        }
    });
    

    That didn’t work, so I tried the following with my custom path:

    UseStatusCodePagesWithReExecute("/notfound");
    

    That didn’t work either. I'm not really sure if its because Umbraco core has already registered middleware and I can’t override/supersede that or that my registration sequence is incorrect (i did try things in different order), so any thoughts/ideas would be welcome.

    I see that the donet site says its running .net 6 RC and a request for a static file results in a custom 404 https://dotnet.microsoft.com/missing.png

    Thanks

    Andrew

  • andrew shearer 510 posts 659 karma points
    Oct 26, 2021 @ 02:00
    andrew shearer
    0

    on https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-5.0, MS say this:

    UseStatusCodePages isn't typically used in production because it returns a message that isn't useful to users.

  • andrew shearer 510 posts 659 karma points
    Oct 28, 2021 @ 04:11
    andrew shearer
    0

    anyone have any thoughts? thanks

  • Hannes 28 posts 189 karma points
    Sep 25, 2023 @ 20:35
    Hannes
    0

    Hi Andrew, I'm facing the same challenge right now. Did you ever find a solution for this? Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft