This project is read-only.

Routes created at runtime

Topics: ASP.NET Web API
Mar 20, 2014 at 6:07 PM
I am creating an extensible set of web apis which allow a user to install a 'module' via the api.
This 'module' would then add its own specific routes to the api at a given url. Ie. all of the routes for module A would be based at modules/module-a (defined outside of the module)

Using the latest nightly, I am able to configure the RoutePrefix for the routes in module A so that they are based at 'modules/module-a'.

From my current understanding, once WebApi is initialized, no more routes can be registered.
Is this correct? Are there plans to be able to register routes after the application is initialized?
Mar 20, 2014 at 7:55 PM
Yes...that's right, once they are initialized no more route routes can be registered...

No, we currently do not have plans to register routes after application initialization...

Can you please elaborate more about your aren't all your modules available during initialization?
Mar 20, 2014 at 8:20 PM
I am giving users the ability to go to a website and 'install' a new module.
They would upload a package containing the module, and then it gets extracted and the assemblies get loaded up.
These modules will also have some configuration that will need to be done which is why I would need to attach the routes after initialization.

I could recycle the application pool after I extracted the module, however that would disrupt the users that are actively using the system.
Mar 23, 2014 at 8:41 AM
In general adding components at runtime to an app would involve adding the component's DLL files to the /bin directory of the app (assuming it's hosted on ASP.NET/IIS) and that would restart the app anyway. The same is true if the app's web.config file is modified. As such, many such dynamic systems would end up having to restart anyway (though there are ways around this by avoiding such changes).

So, even though what kichalla said is true regarding modifying the route collection while the app is running, there are alternatives. Web API's routing system assumes that it is fully configured before the first request is served, but that doesn't mean you can't put in your own custom route that is itself dynamic. For example, you could create your own CustomDynamicSuperAwesomeRoute that derives from IHttpRoute (or whatever the base class / interface is), and in that route you could have your own dynamic set of other routes that you delegate to. Or just do something totally custom as well instead of delegation. Please note that you'll have to do your own locking (such as using ReaderWriterLockSlim) to ensure thread safety. Also there are other potential consequences to this, such as the Web API Help Page not working by default (because it only understands the built in route), and you also can't use attribute routing by default with your new custom route.

So yes it is supported but it's a bit more work.
Mar 26, 2014 at 9:54 PM
I have already gotten around IIS restarting when new modules are added, and I do have a prototype working with a custom route (based on, however I was hoping to be able to use attribute routing.

I've attempted to manually search through the controllers and build the routes based on the Route and RoutePrefix attributes, however it appears that somewhere deep down (cant remember where exactly at this moment) it was using the template from the attribute and not the one that I specified on the route.