We regularly rebuild our route table while our web application is running to add new custom urls. We've recently added some functionality using the WebAPI. After doing a:
Routes.Clear();
If I try and regen the route table by calling:
GlobalConfiguration.Configure(WebApiConfig.Register);
Everything breaks with the delightfully useless error:
Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code
Note that the site initially works and I used GlobalConfiguration.Configure
and am using the correct registration according to questions here and MS documentation.
What is the correct way to rebuild the route table while the site is running while using the Web API?
EDIT: The reset looks roughly like this
'Reset method
Public Sub RebuildRouteTable()
RouteTable.Routes.Clear()
Initech.Web.Bootstrapper.Register(System.Web.Http.GlobalConfiguration.Configuration, RouteTable.Routes)
'Old manual routes to prettify urls
InitechCore.Routes.AddLocationSearchUrl(RouteTable.Routes)
InitechCore.Routes.RegenerateAliasURLCache(RouteTable.Routes)
InitechCore.Routes.RegenerateSiteURLCache(RouteTable.Routes)
InitechCore.Routes.RegenerateURLCache(RouteTable.Routes)
InitechCore.Routes.RegenerateCountyHomeURLCache(RouteTable.Routes)
InitechCore.Routes.RegenerateLandingPageCache(RouteTable.Routes
End Sub
The MVC/Web API registering in C#:
//the bootstrapper, we're migrating an ASP.Net VB site to a C# ASP.Net MVC one
//but this doesn't seem to have anything to do with the problem
//as everything worked until we added the web API
//and even now everything works great until we call RebuildRouteTable
public static void Register(HttpConfiguration config, RouteCollection routes)
{
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
//if I simply comment out this line everything works when we reset the routes
//apart from obviously the Web API routes are no longer registered
//I have also tried only calling WebApiConfig.Register after the initial registration
GlobalConfiguration.Configure(WebApiConfig.Register);
config.Services.Add(typeof(IExceptionLogger), new ExceptionHandler());
routes.MapMvcAttributeRoutes();
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
}
}