Copied to clipboard

Flag this post as spam?

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


  • Ross 16 posts 107 karma points
    Mar 25, 2019 @ 17:06
    Ross
    0

    Custom routes - IPublishedContent Url error

    Hi guys,

    I have implemented a custom route "/performer/{performer-name}" that I have mapped to a CMS page with actual url "/performers/performer". I have followed the guides available for Umbraco 8 and have implemented a RegisterCustomRouteComposer logic to be able to register my route.

    The issue I have is that top navigation razor logic fails on the page in question with the following:

      [ArgumentException: Value cannot be null or whitespace. Parameter name: text] Umbraco.Web.Routing.UrlInfo..ctor(String text, Boolean isUrl, String culture) +153 Umbraco.Web.Routing.CustomRouteUrlProvider.GetUrl(UmbracoContext umbracoContext, IPublishedContent content, UrlProviderMode mode, String culture, Uri current)
    

    This is thrown in the topNavigation partial, the Url property of PublishedContentWrapped:

    <a class="nav-link" href="@item.Url">@item.Name</a> // item is IPublishedContent
    

    Weirdly, this only becomes an issue after an app pool / website recycle. Rebuilding the solution with a trivial change fixes it..

    Custom Route Composer:

    public class RegisterCustomRouteComposer : ComponentComposer<RegisterCustomRouteComponent>
    {
    }
    
    public class RegisterCustomRouteComponent : IComponent
    {
        public void Initialize()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    
        public void Terminate()
        {               
        }
    
        private static void RegisterRoutes(RouteCollection routes)
        {
            RouteTable.Routes.MapUmbracoRoute("Performer details", "performer/{performername}", new
            {
                controller = "Performer",
                action = "Index", 
                performername = UrlParameter.Optional
            }, new PerformerNodeRouteHandler());
        }
    }   
    

    Virtual Route Handler:

    public class PerformerNodeRouteHandler : UmbracoVirtualNodeRouteHandler
    {
        protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
        {
            var performerPage = umbracoContext.ContentCache.GetAtRoot().First().Descendant<Performer>();
            return performerPage;
        }
    }
    

    Controller:

        public class PerformerController : Umbraco.Web.Mvc.RenderMvcController
    {
        // GET: Performer page
        public ActionResult Index(ContentModel model, string performername)
        {
            var viewModel = new PerformerViewModel(model.Content){PerformerName = performername };
            return CurrentTemplate(viewModel);
        }
    
    }
    

    Any help on this would be much appreciated!

    Thanks, Ross

  • Ross 16 posts 107 karma points
    Mar 26, 2019 @ 09:01
    Ross
    0

    Also noticed that in the controller index action above, the "model.Content.Url" (ContentModel type argument) also throws the same error when debugging.

  • shekhar sharma 22 posts 110 karma points
    Jun 14, 2021 @ 12:45
    shekhar sharma
    0

    Hi Ross,

    Not Sure if this Works. Just remove the Blank Space and replace it with "-" hyphen in the {performername} .

    RouteTable.Routes.MapUmbracoRoute("Performer details", "performer/{performername(in this field space need to be replace with hyphen )}", new

    Thanks,

    Shekhar

  • 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