我正在使用 WebAPI 2.0 中包含的属性路由,但无法弄清楚如何根据某些条件删除路由。我使用 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()
执行此操作的功能。 http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-21#ignoreroute
// disable the flag routes if required
if (DisableFlagEndpoint)
{
httpConfiguration.Routes.IgnoreRoute("Flags", "api/flags/{*paths}");
}
// enable attribute routing support
httpConfiguration.MapHttpAttributeRoutes();
关于c# - 从 HttpRouteCollection 中删除路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20895987/