I am using the attribute routing included in WebAPI 2.0, but cannot figure out how to remove a route based on certain conditions. I map all routes using the MapHttpAttributeRoutes
, and then I would like to remove a specific route using the next line of code.
// enable attribute routing support
httpConfiguration.MapHttpAttributeRoutes();
// expose the flag routes only if required
if (DisableFlagEndpoint)
{
httpConfiguration.Routes.Remove(FlagsController.RouteName);
}
But this throws a NotSupportedException
. How does one remove a route ? If not, is there another way to achieve this ?
Looks like WebAPI 2.1 introduces the ability to do this with 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();