Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering dynamic routes, needs an app restart?

I am working on a small CMS for fun and as part of this I register routes from a database at app start. It is possible for a user to add a route themselves. The problem is that this route is stored in the db and so isn't registered until app restart.

Is it possible to re-register routes without an app restart?

If not, how do I restart the app on demand?

Cheers,

Ian

like image 966
madcapnmckay Avatar asked Oct 18 '10 11:10

madcapnmckay


1 Answers

No, you can add and remove routes dynamically. RouteTable.Routes is simply a RouteCollection which has Add and Remove members (or, if you prefer, Clear).

Be aware that the web-server is multithreaded, however, so you'll need to use the RouteCollection's locking protocol. In particular, that means GetWriteLock:

var routes = RouteTable.Routes;
var newDynamicRoute = new Route(...);
using(routes.GetWriteLock()) {
    routes.Remove(dynRoute);
    dynRoute = newDynamicRoute;
    routes.Add(dynRoute);
}
like image 54
Eamon Nerbonne Avatar answered Sep 30 '22 20:09

Eamon Nerbonne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!