Copied to clipboard

Flag this post as spam?

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


  • GianniDPC 13 posts 79 karma points
    Feb 16, 2024 @ 13:38
    GianniDPC
    0

    ContentLastChanceFinder and custom 404 error page issue when using custom routes

    Hi,

    I have an application where I have registered some MVC Routes independent of Umbraco. The issue here is that this causes the ContentLastChanceFinder to be fired because these routes do not have an IPublishedContent set.

    The expected behaviour is that my custom route matches the right controller whereas now the ContentLastChanceFinder will set the 404 page as the IPublishedContent which for some reason causes the 404 page to be hit.

    How can I somehow make my custom route to have precedence and it not hit the ContentLastChanceFinder, as I would expect it only to be hit if nothing was found in the Routing Table.

    Kind regards

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 06, 2024 @ 10:43
    Andy Butland
    0

    Funnily enough we just had the same issue, and I was Googling and found your question.

    At least for us we resolved it by looking at the order in which endpoints were registered.

    We had this code in Program.cs:

    app.UseUmbraco()
    .WithMiddleware(u =>
    {
        u.UseBackOffice();
        u.UseWebsite();
    })
    .WithEndpoints(u =>
    {
        u.UseInstallerEndpoints();
        u.UseBackOfficeEndpoints();
        u.UseWebsiteEndpoints();
        u.UseApplicationEndpoints();
    });
    

    UseApplicationEndpoints is our own extension method where we register custom routes.

    We fixed the problem by moving this method call before u.UseWebsiteEndpoints();

    Maybe that'll solve your issue too.

    Andy

  • GianniDPC 13 posts 79 karma points
    Mar 11, 2024 @ 09:41
    GianniDPC
    0

    Hi Andy,

    How did you register your custom routes?

    I tried moving my custom code after UseBackOfficeEndpoints (so before UseWebsiteEndpoints) but to no avail.

    Another question: do you have a LastChanceContentFinder? and if so does it hit the TryFindContent when navigating to your custom route?

    Kind regards

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 11, 2024 @ 09:47
    Andy Butland
    0

    The UseApplicationEndpoints() extension method looks like this:

    public static IUmbracoEndpointBuilderContext UseApplicationEndpoints(this IUmbracoEndpointBuilderContext builder)
    {
        if (!builder.RuntimeState.UmbracoCanBoot())
        {
            return builder;
        }
    
        // Register route for blog RSS feeds.
        builder.EndpointRouteBuilder.MapControllerRoute(
            "BlogRss",
            "blog/rss/{category?}",
            new
            {
                controller = "BlogRss",
                action = "Index"
            });
    
        // More routes...
    
        return builder;
    }
    

    But not using a LastChanceContentFinder so perhaps your issue is different to the one we ran into.

  • GianniDPC 13 posts 79 karma points
    Mar 11, 2024 @ 10:04
    GianniDPC
    0

    Hi Andy,

    How do you handle 404 pages then? Via appsettings.json? If so could you check if the assignedcontent is also your 404 page?

    You can do this anywhere via the IUmbracoContextAccesor and accessing PublishedRequest.PublishedContent

    Because even if you don't specify your own LastChanceContentFinder umbraco seems to register one on it's own (If you specify the 404 page in appsettings.json at least)

Please Sign in or register to post replies

Write your reply to:

Draft