c# - 名为 'DefaultApi' 的路由已经在路由集合中

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

这个问题可能看起来重复,但略有不同。 在 SO 的所有其他问题中,我注意到他们注册了多条路线。但就我而言,我只有一条路线。

我正在创建 asp.net webapi(框架 4.5)并且在 RegisterRoutes() 方法中只有一个路由 -

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "DefaultApi",
            url: "rest/{controller}/{id}",
            defaults: new { id = UrlParameter.Optional }
        );

    }

那为什么会报错呢?

A route named 'DefaultApi' is already in the route collection. Route names must be unique. Parameter name: name

最佳答案

我在添加路由 DefaultApi 时遇到了类似的问题。尽管我的 ArgumentException 堆栈跟踪中的确切“其他详细信息”消息是:

A route named 'MS_attributerouteWebApi' is already in the route collection.
Route names must be unique.

我当然确保我只添加了一次 DefaultApi 路由,但最后注意到在 Global.asaxApplication_Start 中方法我调用了 WebApiConfig.Register(..) 两次,尽管在下面 - 不是很明显 - 方式:

WebApiConfig.Register(GlobalConfiguration.Configuration);
GlobalConfiguration.Configure(WebApiConfig.Register);

另一个严重的“抄袭者”案例!我只是删除了 WebApiConfig.Register(..) 行并解决了我的问题。

(我使用的是 WEB API 2.0/.NET 5)

关于c# - 名为 'DefaultApi' 的路由已经在路由集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20393265/

相关文章:

c# - 使用 C# 在 RichTextBox 中突出显示语法

c# - 在 asp.net mvc 中重命名 ReturnUrl 文字

c# - C# 中的用户可扩展访问者模式

c# - 在 ASP.NET 中单击链接按钮时如何在 Bootstrap Modal 中显示数据

asp.net - Web API 自定义过滤器问题

c# - 根据 Notepad++ 中的列提取重复行(或通过 C#)

asp.net - 如何从 ASP.net 页面访问主题中的本地资源?

javascript - 使用 ASP 复选框和 JavaScript 折叠表行

c# - 使用 OWIN 将 Web API 托管为 Windows 服务

model-binding - ASP.NET Web API Controller 无法处理动态对象?