Copied to clipboard

Flag this post as spam?

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


  • Ben 112 posts 398 karma points
    May 01, 2024 @ 19:43
    Ben
    0

    Difficulties with custom 404 page

    I am use Umbraco v13.3.0 and uSkinned v6. I am trying to send 404 errors to a custom page I have created. I have added the Error404Collection property to the appsettings.json file, but it doesn't seem to do anything. I have tried with ContentXPath, ContentId, and ContentKey.

    I have a custom middleware class for catching and logging errors in custom code. I found I was able to redirect to my custom 404 page by doing something like this, but I don't really like relying on JavaScript for this.

    Note: context.Response.Redirect was also not working in the below class as I expected.

    public async Task InvokeAsync(HttpContext context){
            try
            {
                await _next(context);
    
                if(context.Response.StatusCode == (int)HttpStatusCode.NotFound)
                {
                    string redirecTo = "/my404";
                    _logger.LogError("trying to redirect (404) from " + context.Request.Path);
    
                    //context.Response.Redirect(redirecTo);
    
                    await context.Response.WriteAsync($"<script>window.location='{redirecTo}';</script>");
                }
            }
            catch (Exception ex)
        {
              //log errors here
            }
    }
    

    I feel like I am missing something. Does anyone have any suggestions on what I can try next?

  • Huw Reddick 1815 posts 6249 karma points MVP 2x c-trib
    May 02, 2024 @ 08:49
  • Ben 112 posts 398 karma points
    May 02, 2024 @ 11:39
    Ben
    0

    Unfortunately, that isn't working either. I can go directly to the custom 404 page, but otherwise when a 404 is thrown, Umbraco shows the default Umbraco 404 page.

  • Ben 112 posts 398 karma points
    May 03, 2024 @ 19:47
    Ben
    0

    I think part of the problem is that when the middleware first catches the context, the response is 200 at that point and not 404. It changes to a 404 status after the "await _next(context);" line. At that point I cannot redirect the response since it has started and the headers have already been sent to the browser. At least that is my current understanding, I could be wrong.

    For the appsettings.json Error404Collection property, I haven't figured out why that one isn't working yet.

  • Ben 112 posts 398 karma points
    May 03, 2024 @ 20:40
    Ben
    100

    I found this video and have successfully redirected my 404s to a custom page: https://www.youtube.com/watch?v=70sJKuP9XF8

    I had to use the Composer and Content finder approach, around the 12:35 minute mark.

    The code looks like this:

    Composer class:

    using MyUmbracoProject.ContentFinder;
    using Umbraco.Cms.Core.Composing;
    
    namespace MyUmbracoProject.Composer;
    
    public class RegisterUmbracoComponents : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            builder.SetContentLastChanceFinder<PageNotFoundContentFinder>();
        }
    }
    

    Content Finder class:

    using Umbraco.Cms.Core.Routing;
    using Umbraco.Cms.Core.Web;
    
    namespace MyUmbracoProject.ContentFinder;
    
    public class PageNotFoundContentFinder : IContentLastChanceFinder
    {
        private readonly IUmbracoContextAccessor _umbracoConextAccessor;
    
        public PageNotFoundContentFinder(IUmbracoContextAccessor umbracoConextAccessor)
        {
            _umbracoConextAccessor = umbracoConextAccessor;
        }
    
        public Task<bool> TryFindContent(IPublishedRequestBuilder request)
        {
            var notFoundPage = _umbracoConextAccessor.GetRequiredUmbracoContext().Content.GetById(1653);
            request.SetIs404();
            request.SetPublishedContent(notFoundPage);
    
            return Task.FromResult(true);
        }
    }
    

    1653 above is the Id for my custom 404 page.

Please Sign in or register to post replies

Write your reply to:

Draft