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)?
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)
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
{
}
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
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(......)
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.
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
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 :
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!
Edit: Oops, no Umbraco does add routes with a handler of type MvcRouteHandler.
Another option is to dig to the Umbraco source to see which route it registers and do this again.
Dave
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.:
Or even simpler:
thanks
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
What Umbraco version are you using and how are you updating the routes ?
Dave
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:
thanks
is working on a reply...