Copied to clipboard

Flag this post as spam?

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


  • Neil Hartley 10 posts 31 karma points
    Aug 17, 2015 @ 15:16
    Neil Hartley
    0

    Custom Route missing from route values

    Ive set up a custom route and initialized it on ApplicationStarted as below.

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            RouteTable.Routes.MapUmbracoRoute("test",
                "In-Memory/{id}",
                new { controller = "InMemoryPage", action = "InMemoryPage", id = UrlParameter.Optional },
                new RouteHandler(ApplicationConstants.InMemoryPageNodeId));
    
            base.ApplicationStarted(umbracoApplication, applicationContext);
        }
    

    When I open the application if I go straight to the URL I want routed I get the following error when the following code gets called.

    CurrentPage.AncestorOrSelf(1).Children.Where("Visible")
    

    Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request

    which comes from a call to Umbraco.Web.Mvc.SurfaceController.get_CurrentPage() I think.

    The weird thing is, If I visit a standard page like the home page first and then go to my custom route, all works as expected..!

    Having spent most of the day on this one, if anyone has any ideas I'd love to hear them :)

    Thanks

  • David Verberckmoes 46 posts 77 karma points
    Aug 18, 2015 @ 13:18
    David Verberckmoes
    0

    Hi Neil,

    does your controller inherits from RenderMvcController?

  • Neil Hartley 10 posts 31 karma points
    Aug 19, 2015 @ 08:50
    Neil Hartley
    0

    Hey David,

    We have the controller inheriting from a page base controller which in turn inherits from SurfaceController, IRenderMvcController

    Thanks

  • David Verberckmoes 46 posts 77 karma points
    Aug 19, 2015 @ 14:57
    David Verberckmoes
    0

    Hi Neil,

    let your base controller inherit from RenderMvcController and add a method like this:

    protected T CreateModel<T>() where T : RenderModel, new()
    {
        var viewModel = new T()
        {
            // you can set properties here if you are inheriting from other interfaces
        };
    
        //add an umbraco data token so the umbraco view engine executes
        RouteData.DataTokens["umbraco"] = viewModel;
    
        return viewModel;
    }
    

    You can also add this to the base controller to have the Index action required in all controllers:

    public abstract override ActionResult Index(RenderModel model);
    

    In your controller inheriting from the base controller you will have something like this:

    public override ActionResult Index(RenderModel model)
    {
        // do lookups ...
    
        var vm = CreateModel<MyViewModel>();
        // set properties for your viewmodel
    
        return CurrentTemplate(vm);
    }
    
  • Simon 692 posts 1068 karma points
    Sep 20, 2016 @ 07:21
    Simon
    0

    Hi Neil,

    How have you solved your problem?

    Thank you for your sharing

    Kind Regards

Please Sign in or register to post replies

Write your reply to:

Draft