Copied to clipboard

Flag this post as spam?

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


  • andrew shearer 506 posts 653 karma points
    Sep 05, 2015 @ 05:36
    andrew shearer
    0

    Umbraco MVC routes - can they be re-registered?

    hi - I have an umbraco website that also registers lots of custom routes. These routes change at runtime, and without going into detail, what would be simplest is to trigger a rebuild of the route table. I tried this code, but that also toasts routes that umbraco has registered.

            var routes = RouteTable.Routes;
            using (routes.GetWriteLock())
            {
                routes.Clear();
                // repopulate route table here
            }
    

    Is there a public method I can call to get Umbraco to re-register what it needs (i.e. all backoffice and content routes)?

    thanks

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Sep 05, 2015 @ 07:56
    Dave Woestenborghs
    1

    We do the same in our applications. We have custom routes that are handled by a virtualnodehandler.

    This code we use to delete our custom routes :

      var pagingRoutes = routes
                    .OfType<Route>()
                    .Where(x => x.RouteHandler.GetType() == typeof(CustomVirtualRouteHandler)).ToArray();
    
                foreach (var route in pagingRoutes)
                {
                    routes.Remove(route);
                }
    
  • andrew shearer 506 posts 653 karma points
    Sep 05, 2015 @ 21:48
    andrew shearer
    0

    Hi Dave – thanks for the info!

    My understanding of what you’re saying is not to rebuild the entire route table but just target the routes by type that need to be replaced? (and therefore leaving the Umbraco registrations as is)

    I’m don’t have any custom route handlers as yet so couldn’t target a type like you have without refactoring a lot of code. (the handler backing all of my routes is MvcRouteHandler). Actually, I think all Umbraco route registrations are against custom umbraco handlers, so maybe I can target “MvcRouteHandler” safely after all? (i.e. everything I’ve added will be against MvcRouteHandler, and everything Umbraco has added won’t be). My other thought could be to target Routes using something in the route metatdata (i.e. route token value such as the controller name) and then re-add all of the routes in that Area?

    It’s important that I rebuild all of the routes as due to the nature of them I need to ensure a ‘depth-first’ sequence is followed. (rather than simply adding a new route or deleting an existing one)

    Thanks!

  • andrew shearer 506 posts 653 karma points
    Sep 05, 2015 @ 21:52
    andrew shearer
    0

    Edit: Oops, no Umbraco does add routes with a handler of type MvcRouteHandler.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Sep 06, 2015 @ 07:08
    Dave Woestenborghs
    0

    Another option is to dig to the Umbraco source to see which route it registers and do this again.

    Dave

  • andrew shearer 506 posts 653 karma points
    Sep 08, 2015 @ 00:19
    andrew shearer
    0

    Hi Dave, yeah that is one thought and I had considered that, but I keep thinking it would probably be safer not to rely on Umbraco plumbing if I can help it.

    I can achieve the Type-based logic you first posted by creating a custom handler that serves no other purpose but to be able to find routes by handler type i.e.:

        public class MyCuystomRouteHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return new MvcHandler(requestContext);
        }
    }
    

    Or even simpler:

        public class MyCustomRouteHandler : MvcRouteHandler
    {
    }
    

    thanks

  • andrew shearer 506 posts 653 karma points
    Feb 24, 2016 @ 03:40
    andrew shearer
    0

    hi again Dave, this is largely working for me - i can target and delete routes and re-add new ones, but i've noticed that the first time i access one of the new routes it fails with a 404, but the second and subsequent times it's fine. Have you noticed anything like this? thanks

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Feb 24, 2016 @ 07:22
    Dave Woestenborghs
    0

    What Umbraco version are you using and how are you updating the routes ?

    Dave

  • andrew shearer 506 posts 653 karma points
    Feb 24, 2016 @ 19:52
    andrew shearer
    0

    this particular site is umbraco 7.3.2.

    I update the Routes by calling a method from the umbraco "CacheUpdated" event. This method removes existing routes of a certain type (basically the code snippet you had provided) and then targets certain Areas so that i can register new routes:

        var myAreaContext = new AreaRegistrationContext("areaNameHere", routes, null); 
        myAreaContext.MapRoute(......)
    

    thanks

Please Sign in or register to post replies

Write your reply to:

Draft