c# - 从 HttpRouteCollection 中删除路由

标签 c# asp.net-web-api-routing asp.net-web-api2

我正在使用 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/

相关文章:

c# - 如何删除计数最低的子列表并保留主列表中计数最高的子列表?

c# - ASP.Net Core 2 如何在身份验证后触发/调用代码

c# - 具有多个参数的 Web API 路由

asp.net-web-api - 没有 Controller 后缀的 Controller

c# - ASP.net 网络 API 2 错误 "The parameters dictionary contains a null entry for parameter"

asp.net - 如何为移动客户端返回 url 中的 oauth token

c# - 使用正则表达式删除文本部分 C#

c# - 使用 Visual Studio 更新 ClickOnce 提示

c# - 在 C# Web API 中通过 HttpRoute 改变 Unity 注入(inject)

c# - 使用媒体类型对 ASP.NET Web API 2 进行版本控制