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 29, 2019 @ 14:24
    Ross
    0

    Custom routes - parameter for Controller action

    Hi,

    Anyone know what is the equivalent to the "RenderModel model" parameter in version 8 that needs to be passed in to a controller action? I'm using "ContentModel" at the moment and I can get the content I've mapped the request to. However, "model.Url" throws an error:

    ((Umbraco.Core.Models.PublishedContent.PublishedContentWrapped)item).Url' threw an exception of type 'System.ArgumentException
    

    Controller action:

            public ActionResult Index(ContentModel model, string performername, string section)
        {
            Performer performer = PerformerService.GetPerformer(performername);
            var viewModel = new PerformerViewModel(model.Content);
            this.SetViewModel(viewModel, performer, section);
            //var url = model.Content.Url; --> this throws the error above
            return CurrentTemplate(viewModel);
        }
    

    I've mapped the route using RouteTable.Routes.MapUmbracoRoute (and implemented a UmbracoVirtualNodeRouteHandler.

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Mar 30, 2019 @ 11:32
    Marc Goodson
    0

    Hi Ross

    ContentModel is the correct thing to have, and it's the job of the UmbracoVirtualNodeRouteHandler to return the IPublishedContent item that will be associated with the root.

    I've just spun up a quick test in V8, and I'm finding the model is populated and the Url is being returned without error:

    enter image description here

    I'm using the core UmbracoVirtualNodeByIdRouteHandler that takes the Id of the content item you want to associate, here 1105 is the Products node in the starter kit

    public void Initialize()
    {
        RouteTable.Routes.MapUmbracoRoute("ProductCustomRoute", "products2/{action}/{id}", new
        {
            controller = "SuperProduct",
            id = UrlParameter.Optional
        }, new UmbracoVirtualNodeByIdRouteHandler(1105));
    }
    

    so maybe the issue is inside your implementation of the VirtualNodeRouteHandler?

    eg is that definately finding and returning a published content item?

  • Ross 16 posts 107 karma points
    Apr 01, 2019 @ 12:12
    Ross
    0

    Hi Marc,

    thanks for checking and confirming this. My implementation of VirtualNodeRouteHandler is simple and it returns the content the request is mapped to:

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

    I've also tried your approach using UmbracoVirtualNodeByIdRouteHandler directly in "MapUmbracoRoute" with the same result.

    I thought it maybe something to do with the fact I've moved my models to a separate project so I moved them back to the web project (App_Data) folder - that didn't help either. My current config is:

    <add key="Umbraco.ModelsBuilder.ModelsMode" value="AppData" />
    <add key="Umbraco.ModelsBuilder.ModelsNamespace" value="UmbracoEvaluation.Entities.DocumentTypes" /> 
    <add key="Umbraco.ModelsBuilder.ModelsDirectory" value="~/../UmbracoEvaluation.Entities/DocumentTypes" />
    

    The strange thing is that in between builds I can get this to work, however if the app pool recycles (or I force it to), it will start to fail..

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Apr 04, 2019 @ 18:39
    Marc Goodson
    0

    Hi Ross

    That does sound strange.

    If you set a breakpoint on your return performerPage; line in your VirtualNodeRouteHandler, and the scenario reoccurs, is the Url property also null on the performerPage object?

    eg trying to work out if all is ok with the VirtualNodeRouteHandling + MapUmbracoRoute, or whether the issue lies with your published cache somehow... hmmm

    regards

    Marc

  • Ross 16 posts 107 karma points
    Apr 08, 2019 @ 10:27
    Ross
    0

    Hi Marc,

    this is resolved now. I made two changes so unfortunately cannot pinpoint this to a specific one. What I did was upgrade to CMS 8.0.1 & ModelsBuilder 8.0.4 (both my web and entities projects).

    I also changed the way my Entities project was referencing Umbraco dlls - previously I had simply added references to Umbraco CMS & ModelsBuilder, pointing to the relevant packages:

        <Reference Include="Umbraco.Core">
      <HintPath>..\packages\UmbracoCms.Core.8.0.0\lib\net472\Umbraco.Core.dll</HintPath>
    </Reference>
    <Reference Include="Umbraco.ModelsBuilder">
      <HintPath>..\packages\Umbraco.ModelsBuilder.8.0.1\lib\net472\Umbraco.ModelsBuilder.dll</HintPath>
    </Reference>
    <Reference Include="Umbraco.Web">
      <HintPath>..\packages\UmbracoCms.Web.8.0.0\lib\net472\Umbraco.Web.dll</HintPath>
    </Reference>
    

    Now that I'm managing these through NuGet for the Entities project, it has resulted in NuGet pulling quite a few more dependencies, so I am inclined to thing it's the latter.

    Thanks for you help. Ross

Please Sign in or register to post replies

Write your reply to:

Draft