1

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();
    }
}
Community
  • 1
  • 1
mattmanser
  • 5,719
  • 3
  • 38
  • 50
  • what do you mean by 'regenerating' the route table? Could you share how your `RouteConfig.cs`, `WebApiConfig.cs` and `Global.asax.cs` files or their equivalent configuration? – Kiran Mar 04 '14 at 11:47
  • @KiranChalla I've updated the question with what we're doing – mattmanser Mar 04 '14 at 12:02
  • @KiranChalla I also forgot to say that we exclusively use the Route attribute to register the MVC and API routes at the moment to give us granular control – mattmanser Mar 04 '14 at 12:06
  • We are facing exactly the same problem. Did you solve this issue? – ipinak Oct 08 '15 at 09:01
  • 1
    @ipinak I'm afraid to say we ended up restarting IIS whenever we needed to reset the routes as it was fairly infrequent and we didn't want to spend the time figuring out what was wrong with WebAPI – mattmanser Oct 08 '15 at 20:40
  • 1
    Thanks a lot we were thinking of doing the same thing. – ipinak Oct 09 '15 at 07:20

0 Answers0