重建路由表会导致Web API炸毁整个站点

时间:2014-03-04 11:40:25

标签: c# asp.net asp.net-web-api asp.net-mvc-routing

我们在运行Web应用程序时定期重建路由表以添加新的自定义URL。我们最近使用WebAPI添加了一些功能。做完之后:

Routes.Clear();

如果我尝试通过调用以下方法重新生成路由表:

GlobalConfiguration.Configure(WebApiConfig.Register);

一切都打破了令人愉快的无用错误:

Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code

请注意,该网站最初有效,我使用了GlobalConfiguration.Configure,并根据questions hereMS documentation使用了正确的注册。

使用Web API时,在网站运行时重建路由表的正确方法是什么?

编辑:重置看起来大致如此

'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

在C#中注册的MVC / Web API:

//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();
    }
}

0 个答案:

没有答案