Copied to clipboard

Flag this post as spam?

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


  • Simon 691 posts 1066 karma points
    Sep 19, 2016 @ 13:02
    Simon
    0

    Cannot find the Umbraco route definition in the route values when using custom Routes

    Hi Guys,

    I have experiencing an error when using custom routes.

    I have the below custom route:

     public class CustomRoutesApplicationEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                RouteTable.Routes.MapUmbracoRoute(
                        ConstantValues.ProductsSearchCustomRouteName,
                        "search/{" + ConstantValues.ProductsCategoriesRouteParamName + "}/",
                        new
                        {
                            controller = "Products",
                            action = "Products"
                        },
                        new UmbracoVirtualNodeByIdRouteHandler(ConstantValues.ProductsPageId)
                    );
    
            }
        }
    

    When I get the full url for the route url and acutally redirect to that url, I am getting the below error:

    Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.InvalidOperationException: Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request
    

    Can anyone give me some insight about this?

    Thank you in advance.

    Kind Regards

  • Simon 691 posts 1066 karma points
    Sep 20, 2016 @ 07:13
    Simon
    0

    Hi Guys,

    My Problem is that when I am trying to use custom Routes, when trying to access the CurrentPage it is throwing the above error.

    For some reason, when using custom Routes, when goes back to the surface controller, the CurrentPage is not accessible.

    Any help would be much appreciated.

    Thank you

    Kind Regards

  • alex 10 posts 80 karma points
    Jan 24, 2017 @ 08:07
    alex
    0

    Hi Guys,

    I essentially have the same problem as Simon, that when I use custom routes, the current page property throws an invalid operation exception. Is there a way to set the current page when routing to the custom page by using the code below.

    Here is my custom route:

    public class CustomRoutesApplicationEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,
            ApplicationContext applicationContext)
        {
            RouteTable.Routes.MapUmbracoRoute
                (
                    DatabaseConstantValues.PropertiesSearchCustomRouteName,
                    "properties/{" + DatabaseConstantValues.PropertyCategoryRouteParamName + "}/{" +
                    DatabaseConstantValues.PropertyRegionRouteParamName + "}/{" +
                    DatabaseConstantValues.PropertyLocalityRouteParamName + "}/{" +
                    DatabaseConstantValues.PropertyTypeRouteParamName + "}/",
                    new
                    {
                        controller = "Properties",
                        action = "PropertiesSearch"
                    },
                    new UmbracoVirtualNodeByIdRouteHandler(ConstantValues.PropertiesPageId)
    
                );
        }
    }
    

    Thanks for any help in advance.

    Kind Regards

  • Frans de Jong 480 posts 1648 karma points c-trib
    Jan 24, 2017 @ 08:33
    Frans de Jong
    0

    What are you trying to acomplish?

    We have a service routing for the methods we want available outside umbraco but for all other stuff we use route hijacking. Both ways work, but it looks like you want to combine these two?

    Frans

  • alex 10 posts 80 karma points
    Jan 24, 2017 @ 09:25
    alex
    0

    Hi Frans,

    Essentially what I am trying to do is access the current page property from a custom routed page. The reason I need access to it is because the master page has an action method which uses the current page property and when trying to access it, an invalid operation exception is thrown. This works for any umbraco page however, when used in the page that routed by a custom route the current page throws an exception. If current page is only set when using umbraco page (which I assume it is), then yes, I am essentially trying to partially combine the two together by setting the current page property during the routing method if there is a way. Because the current page property throws the exception even when mimicking an umbraco node during the routing method which is done here:

    UmbracoVirtualNodeByIdRouteHandler(ConstantValues.PropertiesPageId)

            RouteTable.Routes.MapUmbracoRoute
                (
                    DatabaseConstantValues.PropertiesSearchCustomRouteName,
                    "properties/{" + DatabaseConstantValues.PropertyCategoryRouteParamName + "}/{" +
                    DatabaseConstantValues.PropertyRegionRouteParamName + "}/{" +
                    DatabaseConstantValues.PropertyLocalityRouteParamName + "}/{" +
                    DatabaseConstantValues.PropertyTypeRouteParamName + "}/",
                    new
                    {
                        controller = "Properties",
                        action = "PropertiesSearch"
                    },
                    new UmbracoVirtualNodeByIdRouteHandler(ConstantValues.PropertiesPageId)
    
                );
    

    Thanks for your help

    Alex

  • alex 10 posts 80 karma points
    Jan 27, 2017 @ 07:59
    alex
    0

    Hi Frans,

    Sorry to be a bother. But do you know of a way this can be accomplished?

    Thanks,

    Alex

  • Frans de Jong 480 posts 1648 karma points c-trib
    Jan 27, 2017 @ 08:26
    Frans de Jong
    0

    Sorry, didn't see your post.

    I don't think you can access CurrentPage in a custom route because Umbraco doesn't know what you'r doing. (somebody correct me if I'm wrong)

    But I still don't understand what your end goal is. Maybe there is another way without the routing?

    Are you trying to access the properties on a propertiespage or something? I / We need to see where you want to go to help you find the way there.

    Frans

  • alex 10 posts 80 karma points
    Jan 27, 2017 @ 09:23
    alex
    0

    Hi Frans,

    Thanks for your prompt reply. The end goal for this is essentially to load masterpage specific content using the current page property. The reason routing is used is because the listing for properties is loaded from a separate database and not from the umbraco db. Therefore, routing was necessary to load the controller and method which will load the custom page. A umbraco page was mimicked with the custom route only to have access to the document type alias and page id, these are necessary to load the masterpage specific content such as (JS scripts, menu items, etc...).

    Thanks,

    Alex

  • thenortonsetup 2 posts 71 karma points
    Jan 27, 2017 @ 09:56
    thenortonsetup
    0

    You can check this out. If you want to create Custom Routes in Umbraco. Here is the link of article http://maffrigby.com/using-custom-routes-in-umbraco/. May be by this way you can short out your problem.

  • Dave Woestenborghs 3203 posts 10695 karma points MVP 4x admin c-trib
    Jan 27, 2017 @ 10:32
    Dave Woestenborghs
    0

    Hi Simon,

    Is your controller inheriting from SurfaceController or RenderMvcController.

    Virtual routes don't work for Surface controllers.

    Maybe you paste the code of your controller as well ? We use virtual routes quite often and never have issues with it.

    Dave

  • Frans de Jong 480 posts 1648 karma points c-trib
    Jan 27, 2017 @ 11:00
    Frans de Jong
    0

    Why Don't you Hijack the route on the page by having a controller inheriting the RenderMvcController with the name of the document type and use that to get the content you need? This leaves the routing intact and you can append all the info you want to the model.

    https://our.umbraco.org/documentation/reference/routing/custom-controllers

  • Dave Woestenborghs 3203 posts 10695 karma points MVP 4x admin c-trib
    Jan 27, 2017 @ 11:24
    Dave Woestenborghs
    0

    Hi Frans,

    Route Hijacking is something different than virtual routes.

    With route hijacking you built your own controller to handle the routing of a specific doctype so you can add additional logic.

    Virtual routes let's you define your own urls that point to a content item in Umbraco.

    You can combine that with route hijacking.

    Dave

  • Frans de Jong 480 posts 1648 karma points c-trib
    Jan 27, 2017 @ 13:20
    Frans de Jong
    0

    Okay, that's new for me. Will dive into that.

    But if I understand the problem correctly, he wants to show extra information on a existing content page in Umbraco right?

    In that cast the route hijacking way would be the way to go right? The extra content is coming from a different database so the virtual rout isn't the obvious choice right?

    If I didn't understand the problem correctly than I'm out :P

    Frans

  • alex 10 posts 80 karma points
    Jan 27, 2017 @ 14:20
    alex
    0

    That would work, if I was using the umbraco url. The problem is that I have a custom url which holds search parameters, therfore I cannot use the umbraco url. The url has to be custom build to store these search parameters. The items are then retrieved based on those parameters in the url.

    Alex

  • Dave Woestenborghs 3203 posts 10695 karma points MVP 4x admin c-trib
    Jan 27, 2017 @ 14:30
    Dave Woestenborghs
    0

    I asked this before in this thread, but maybe you missed it ? Is your controller for the route inherting SurfaceController or RenderMvcController ?

    Dave

  • alex 10 posts 80 karma points
    Jan 27, 2017 @ 14:38
    alex
    0

    Hi Dave,

    Sorry, I did miss it. Currently my controller inherits from the surface controller. However, I did try it with render RenderMvcController and it still threw an invalid operation exception.

    Alex

  • Dave Woestenborghs 3203 posts 10695 karma points MVP 4x admin c-trib
    Jan 27, 2017 @ 14:48
    Dave Woestenborghs
    0

    Hi Alex,

    It needs to inherit RenderMvcController.

    SurfaceControllers are autorouted so you can not give them a custom route. See docs : https://our.umbraco.org/documentation/Reference/Routing/surface-controllers

    Maybe you can post some code on how you register the route and of your RenderMvcController to see if we can find the source of the issue.

    Dave

  • alex 10 posts 80 karma points
    Jan 30, 2017 @ 12:55
    alex
    0

    Hi Dave,

    Sorry for the long wait. Here is a detailed process of what I am trying to accomplish:

    This code used to route to the properties page:

    public class CustomRoutesApplicationEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,
            ApplicationContext applicationContext)
        {
            RouteTable.Routes.MapUmbracoRoute
                (
                    DatabaseConstantValues.PropertiesSearchCustomRouteName,
                    "properties/{" + DatabaseConstantValues.PropertyCategoryRouteParamName + "}/{" +
                    DatabaseConstantValues.PropertyRegionRouteParamName + "}/{" +
                    DatabaseConstantValues.PropertyLocalityRouteParamName + "}/{" +
                    DatabaseConstantValues.PropertyTypeRouteParamName + "}/",
                    new
                    {
                        controller = "Properties",
                        action = "PropertiesSearch"
                    },
                    new UmbracoVirtualNodeByIdRouteHandler(ConstantValues.PropertiesPageId)
    
                );
        }
    }
    

    This is the controller which is used to handle the routed page, it is only creating a master page model and returning it to the view:

    public partial class PropertiesController : RenderMvcController
    {
        private readonly IUmbracoControllerWrapperService _umbracoControllerWrapperService;
        private readonly IMasterModelService _masterModelService;
    
        public PropertiesController(
            IUmbracoControllerWrapperService umbracoControllerWrapperService,
            IMasterModelService masterModelService)
        {
    
            _masterModelService = masterModelService;
            _umbracoControllerWrapperService = umbracoControllerWrapperService;
        }
    
        public virtual ActionResult PropertiesSearch(
            RenderModel model,
            string category,
            string type,
            string region,
            string locality,
            decimal? priceFrom,
            decimal? priceTo,
            int? amountOfBedrooms,
            string interval,
            string propertyFeatures,
            int? page,
            int? show,
            int? sort,
            int? popularSearchId)
        {
            var umbraco = _umbracoControllerWrapperService.GetUmbracoHelperForUmbracoController(this);
            var mastermodel = _masterModelService.CreateMasterModel(umbraco, model.Content);
            return View(MVC.Properties.Views.PropertiesListingPage, mastermodel);
        }
    }
    

    This is the view used. The problem occurs in the layout which makes use of a child action method that requires the current page:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<RemaxLettings.Code.Models.MasterModel<RemaxLettings.Code.Models.Property.PropertyListingPageModel>> @{
    Layout = "/Views/umbPagesLayout.cshtml";}
    

    I am using this action which is called in the layout umbPagesLayout.cshtml:

    @Html.Action("RenderStyles", "ScriptsAndStyles", true)
    

    This is the method which is causing the invalid operation exception when referencing the current page:

        public virtual PartialViewResult RenderScripts()
        {
            var scriptAndStylesModel = new ScriptsAndStyles()
            {
                CurrentPageId =  CurrentPage.Id,
                DocumentTypeAlias = CurrentPage.DocumentTypeAlias
            };
    
            return PartialView(MVC.Partials.Views.Common._scripts, scriptAndStylesModel);
        }
    

    Thanks for your help,

    Alex

  • Dave Woestenborghs 3203 posts 10695 karma points MVP 4x admin c-trib
    Jan 30, 2017 @ 13:51
    Dave Woestenborghs
    102

    Hi Alex,

    In the last code snippet I asume you are using a SurfaceController

    Can you do this instead of CurrentPage

    this.Umbraco.AssignedContentItem
    

    I also see you use this code to get the UmbracoHelper

    _umbracoControllerWrapperService.GetUmbracoHelperForUmbracoController(this);
    

    In a controller you can do this : this.Umbraco to get access to the umbracohelper.

    Dave

  • alex 10 posts 80 karma points
    Jan 31, 2017 @ 09:06
    alex
    0

    Hi Dave,

    Your solution worked great. Thanks a lot for your help.

    Alex

  • David Peck 587 posts 1553 karma points c-trib
    Jan 05, 2018 @ 14:35
    David Peck
    0

    Dave -- you d'man.

  • Ronald Barendse 20 posts 103 karma points c-trib
    Apr 05, 2019 @ 10:38
    Ronald Barendse
    0

    Although the solution from Dave works, changing the code to use AssignedContentItem instead of CurrentPage might not always be possible...

    You can also just define the missing Umbraco route definition in the UmbracoVirtualNodeRouteHandler:

    protected override void PreparePublishedContentRequest(PublishedContentRequest publishedContentRequest)
    {
        // Add route definition (required in SurfaceController to get the current page)
        var requestContext = publishedContentRequest.RoutingContext.UmbracoContext.HttpContext.Request.RequestContext;
        requestContext.RouteData.DataTokens[global::Umbraco.Core.Constants.Web.UmbracoRouteDefinitionDataToken] = new RouteDefinition()
        {
            ActionName = requestContext.RouteData.GetRequiredString("action"),
            ControllerName = requestContext.RouteData.GetRequiredString("controller"),
            PublishedContentRequest = publishedContentRequest
        };
    
        base.PreparePublishedContentRequest(publishedContentRequest);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft