Custom Route does not work 100%
# help-with-umbraco
c
Has anyone ever encountered the issue where a route works 99% of the time, and only some it goes straight to 404? endpoints.MapControllerRoute( "Quotes by Author", "/quotes/by-{authorSlug}", new { Controller = "QuotesCustom", Action = "QuotesByAuthor" }); But when the url is
/quotes/by-toby-mensforth
or
quotes/by-debby-ryan
or
quotes/by-gaby-hoffmann
it just doesn't reach the controller at all. There are over 90K authors, and most work fine. Any suggestions on how to debug this? https://cdn.discordapp.com/attachments/1319367100636332103/1319370048833323019/quote-2.png?ex=6765b69c&is=6764651c&hm=b22ccf31925b573532e1a64ce299b0f31d3563d711f40154f64a0f01eb8ac4e6& https://cdn.discordapp.com/attachments/1319367100636332103/1319370049185648710/quote-1.png?ex=6765b69c&is=6764651c&hm=eb8562cf9710b3488a5cbc659e700b53a7cc8e90ecaad8e8e15b9fecce7c65b7&
k
We've had a similar problem when part of the route was interpreted as a culture (e.g.,
/home/sv-SE/my-page
) by either ASP or Umbraco, I forget which. Then a "culture specified in URL" mechanism took precedence over our routing. I wonder if something similar could be happening here.
Your two examples have a lot in common (slug2 ends in
by
and is 4 or 5 characters). Do you have any examples that are very different?
c
so far I've only encountered this issue with the 3 slugs I sent. since there are over 90K authors, these are being found at random
k
Make a ps1/cmd that curls them all and see which ones are 404s?
c
ok, I'll do that
k
Also, can you turn on more verbose logging to see which URL is actually sent to Umbraco, and who is returning the 404? If you make a custom last-chance content-finder and debug that, can you see the request details?
c
ok will that that too
just to follow up (after a long break): This works now
Copy code
csharp
public class AuthorConflictContentFinder(IUmbracoContextAccessor umbracoContextAccessor) : IContentFinder
    {
        private readonly IUmbracoContextAccessor _umbracoContextAccessor = umbracoContextAccessor;
        public async Task<bool> TryFindContent(IPublishedRequestBuilder request)
        {
            var path = request.Uri.GetAbsolutePathDecoded();
            string pattern = @"^/quotes/by-([a-z]+(-[a-z]+)*)(\?page=(\d+))?(&topic=([^&]+))?$";
            Regex regex = new(pattern, RegexOptions.IgnoreCase);

            var match = regex.Match(path);
            if (match.Success)
            {
                var authorSlug = match.Groups[1].Value;
                var page = match.Groups[3].Length > 0 ? int.Parse(match.Groups[3].Value) : 1;
                var topic = match.Groups[4].Length > 0 ? match.Groups[4].Value : "";
                var pagination = new Pagination();
                if (pagination.Page == 0) pagination.Page = page;
                if (pagination.PageSize == 0) pagination.PageSize = 14;

                if (_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
                {
                    var contentPage = umbracoContext.Content!.GetAtRoot().DescendantsOrSelf<Quotes>().FirstOrDefault();
                    contentPage!.AuthorSlug = authorSlug;
                    contentPage!.Pagination = pagination;
                    contentPage!.Topic = topic;
                    await Task.FromResult(0);
                    request.SetPublishedContent(contentPage);
                }
                return true;
            }

            return false;
        }
    }
14 Views