2

我正在使用 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. 如何删除一条路线?如果没有,还有其他方法可以实现吗?

4

1 回答 1

2

看起来 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();
于 2014-02-05T12:13:32.480 回答