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
    Jun 24, 2021 @ 13:32
    Robin Hansen
    0

    How to get content from the Url by custom route

    I'm trying to make a custom route to my CategoryItems, rather than using default routing (eg. /experiences/calgary-tower/) which can cause potential dublicates, I rather use custom routing (eg. /experiences/canada/alberta/calgary/calgary-tower/1247)

    I have two problems:

    1. The ActionResult 'CategoryitemRouted' in my controller, if I return the CurrentTemplate I get this error 'No physical template file was found for template CategoryitemRouted'. If I returns the view directly, I cannot inherit anything from my master template. So any of these solutions is'nt quite helpfull :|

    2. How do I get content passed to my Template through the model. I need at least the Id in order to display my content... :-)

    My files:

    //My Composer
    namespace Umbraco8.Project.Composers
    {
        public class RegisterCustomRouteComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<RegisterCustomRouteComponent>();
            }
        }
    }
    
    
    //My Component
    namespace Umbraco8.Project.Components
    {
        public class RegisterCustomRouteComponent : IComponent
        {
            public void Initialize()
            {
                RouteTable.Routes.MapRoute(
                    name: "Category Items Routed",
                    url: "{category}/{country}/{state}/{city}/{displayName}/{id}",
                    defaults: new {
                        controller = "Categoryitem",
                        action = "CategoryitemRouted",
                        state = UrlParameter.Optional
                    });
            }
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
        }
    }
    
    //My Controller
    namespace Umbraco8.Project.Controllers
    {
        public class CategoryitemController : RenderMvcController
        {
            public override ActionResult Index(ContentModel model)
            {
                return CurrentTemplate(model);
            }
    
            public ActionResult CategoryitemRouted(ContentModel model, int id)
            {
                return CurrentTemplate(model);
            //return View("~/views/CategoryitemRouted.cshtml", model);
            }
        }
    }
    
  • Robin Hansen 135 posts 368 karma points
    Jul 16, 2021 @ 12:16
    Robin Hansen
    100

    Hi all, I managed so solve my own problem using this tutorial: https://our.umbraco.com/documentation/reference/routing/custom-routes and it now works as intended.

    My code:

    public class RegisterCustomRouteComponent : IComponent
    {
        private readonly IUmbracoContextFactory _context;
    
        public RegisterCustomRouteComponent(IUmbracoContextFactory context)
        {
            _context = context;
        }
    
        //Real World Example: https://our.umbraco.com/documentation/reference/routing/custom-routes
        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;
        }
    }
    

    Though I'm facing a new challenge! - When I update a node, the changes does'nt show up when i view the page with a custom route, it's it is heavily cached. Any idea how to clear/bypass the cache when using custom routing...?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies