Copied to clipboard

Flag this post as spam?

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


  • Robin Hansen 135 posts 368 karma points
    Aug 13, 2021 @ 09:16
    Robin Hansen
    0

    Odd Caching issue with Custom Routing

    I have followed this example from the documentation regardingCustom Routing, and it works quite well for me: https://our.umbraco.com/documentation/reference/routing/custom-routes

    However if I edit the node in Umbraco the changes does'nt kick in when I render the page via the custom Url. (I do see the changes when using the default Umbraco Url though)

    Is it a bug, or do I miss something...? - my files are listed below :-|

    RegisterCustomRouteComposer.cs:

    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using UmbracoVIII.Core.Components;
    using UmbracoVIII.Core.Controllers;
    
    namespace UmbracoVIII.Core.Composers
    {
        public class RegisterCustomRouteComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<RegisterCustomRouteComponent>();
            }
        }
    }
    

    RegisterCustomRouteComponent.cs:

    using System.Collections.Generic;
    using System.Web.Routing;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.PublishedCache;
    using UmbracoVIII.Core.Helpers.Urls;
    
    namespace UmbracoVIII.Core.Components
    {
        public class RegisterCustomRouteComponent : IComponent
        {
            private readonly IUmbracoContextFactory _context;
    
            public RegisterCustomRouteComponent(IUmbracoContextFactory context)
            {
                _context = context;
            }
    
            public void Initialize()
            {
                using (var cref = _context.EnsureUmbracoContext())
                {
                    IPublishedContentCache umbracoHelper = cref.UmbracoContext.Content;
                    IEnumerable<IPublishedContent> categoryListing = umbracoHelper.GetByXPath("//categoryitem");
    
                    foreach (var item in categoryListing)
                    {
                        RouteTable.Routes.MapUmbracoRoute(
                        name: "CategoryItem-" + item.Id,
                        url: UrlHelpers.GetCategoryItemUrl(item.Id).TrimStart('/'),
                        new
                        {
                            controller = "Categoryitem",
                            action = "Categoryitem"
                        },
                        new CategoryitemHandler(item));
                    }
                }
            }
    
            public void Terminate()
            {
                //throw new NotImplementedException();
            }
        }
    
        public class CategoryitemHandler : UmbracoVirtualNodeRouteHandler
        {
            private readonly IPublishedContent _node;
    
            public CategoryitemHandler(IPublishedContent node)
            {
                _node = node;
            }
    
            protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
            {
                return _node;
            }
        }
    }
    

    CategoryitemController.cs:

    using System.Web.Mvc;
    using Umbraco.Web.Models;
    using Umbraco.Web.Mvc;
    
    namespace UmbracoVIII.Core.Controllers
    {
        public class CategoryitemController : RenderMvcController
        {
            public ActionResult Categoryitem(ContentModel model)
            {
                return CurrentTemplate(model);
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft