C# 重建路由表会导致Web API炸毁整个站点
我们定期在web应用程序运行时重建路由表,以添加新的自定义URL。我们最近使用WebAPI添加了一些功能。在完成以下操作后:C# 重建路由表会导致Web API炸毁整个站点,c#,asp.net,asp.net-web-api,asp.net-mvc-routing,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 applic
Routes.Clear();
如果我尝试通过调用以下命令重新生成路由表:
GlobalConfiguration.Configure(WebApiConfig.Register);
一切都因一个令人愉快的无用错误而中断:
Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code
请注意,该站点最初工作正常,我使用了GlobalConfiguration.configuration
,我使用了与对应的正确注册
使用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:
“重新生成”路由表是什么意思?你能分享一下你的
RouteConfig.cs
,WebApiConfig.cs
和Global.asax.cs
文件或它们的等效配置?@KiranChalla我已经用我们正在做的更新了这个问题doing@KiranChalla我还忘了说,我们目前专门使用Route属性来注册MVC和API路由,以提供粒度control@ipinak我不敢说我们每当我们需要重新设置路由时,就会重新启动IIS,因为这是非常罕见的,我们不想花时间找出WebAPI的问题所在。多亏了我们一直在考虑做同样的事情。
//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();
}
}