【问题标题】:A route named 'DefaultApi' is already in the route collection error名为“DefaultApi”的路由已在路由收集错误中
【发布时间】:2016-01-11 04:03:31
【问题描述】:

尽管尝试了 Stack Overflow 中可用的不同解决方案,但我无法解决此问题。我试过了:

  1. 正在删除项目bin文件夹中的所有dll。
  2. 清理/重建
  3. 重命名项目。
  4. 我尝试在一个 global.asax 中进行验证,但没有发现重复。而且我没有 AreaRegistration.cs 文件。

我的代码:

RouteConfig.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;

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

            routes.MapHttpRoute(
                name: "GetReviewComments",
                routeTemplate: "api/reviews/comments/{id}",
                defaults: new { id = RouteParameter.Optional, controller = "Reviews", action = "Comments" }
            );

            routes.MapHttpRoute(
                name: "GetByCategories",
                routeTemplate: "api/reviews/categories/{category}",
                defaults: new { category = RouteParameter.Optional, controller = "Reviews", action = "GetByCategory" }
            );

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

全球.asax:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Data.Entity;
using Reviewed.Models;

namespace Reviewed
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            Database.SetInitializer(new ReviewedContextInitializer());
        }
    }
}

【问题讨论】:

  • 只需在您的解决方案中搜索字符串“DefaultApi”。我很确定另一个在WebApiConfig.Register()注册。
  • 感谢您的帮助。您可以将其添加为答案。但是我不明白 WebApiConfig 中的路由是做什么的。

标签: c# asp.net-mvc asp.net-web-api


【解决方案1】:

我不明白 WebApiConfig 中的路由是做什么的。

没什么特别的,真的。

只有这样的约定是将启动代码放在 App_Start 目录中的静态类方法中,并从 Global.asax 中调用这些方法。

随着 MVC 5 / WebAPI 2 版本的发布,这一约定发生了细微的变化,因此,如果您遵循有点过时的教程,或者在使用旧版本时针对较新版本的教程,或者在您的项目中升级 MVC 版本,或者添加WebAPI 到现有的 MVC 项目,您最终可能会得到重复或错误的代码。如果要升级,请务必关注upgrade path

鉴于您同时拥有WebApiConfig.Register(GlobalConfiguration.Configuration)RouteConfig.RegisterRoutes(RouteTable.Routes),您似乎已经完成了上述操作。

最终,这将导致此代码:

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

被调用两次,导致你看到的错误。

【讨论】:

    猜你喜欢
    • 2013-12-21
    • 2014-12-10
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 2012-06-14
    • 2018-04-27
    相关资源
    最近更新 更多