C# 从HttpRouteCollection中删除路由

C# 从HttpRouteCollection中删除路由,c#,asp.net-web-api-routing,asp.net-web-api2,C#,Asp.net Web Api Routing,Asp.net Web Api2,我正在使用WebAPI 2.0中包含的属性routing,但无法根据某些条件确定如何删除路由。我使用MapHttpAttributeRoutes映射所有路由,然后我想使用下一行代码删除特定路由 // enable attribute routing support httpConfiguration.MapHttpAttributeRoutes(); // expose the flag routes only if required

我正在使用WebAPI 2.0中包含的属性routing,但无法根据某些条件确定如何删除路由。我使用
MapHttpAttributeRoutes
映射所有路由,然后我想使用下一行代码删除特定路由

        // enable attribute routing support
        httpConfiguration.MapHttpAttributeRoutes();

        // expose the flag routes only if required
        if (DisableFlagEndpoint)
        {
            httpConfiguration.Routes.Remove(FlagsController.RouteName);
        }

但这会抛出一个
NotSupportedException
。如何删除路由?如果没有,是否有其他方法可以实现这一点?

看起来WebAPI 2.1引入了使用
IgnoreRoute()实现这一点的功能


InTesting..您需要执行此操作的具体场景是什么?我想禁用/删除每个环境的路由。例如,测试路由入口只在dev机器上需要,所以我只想在那里启用它,并在所有其他配置中禁用它。我猜我将不得不写一个快速过滤器来实现这一点,虽然。。。
// disable the flag routes if required
if (DisableFlagEndpoint)
{
    httpConfiguration.Routes.IgnoreRoute("Flags", "api/flags/{*paths}");
}

// enable attribute routing support
httpConfiguration.MapHttpAttributeRoutes();