A route named 'DefaultApi' is already in the route collection error

混江龙づ霸主 提交于 2021-01-28 20:13:50

问题


Despite trying the different solutions available in Stack Overflow, I can't resolve this problem. I have tried:

  1. Deleting all dll in bin folder of the project.
  2. Cleaning/Rebuilding
  3. Renaming the project.
  4. I tried verifying in one global.asax but found no duplicate.And I don't have an AreaRegistration.cs file.

My code:

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 }
            );
        }
    }
}

Global.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());
        }
    }
}

回答1:


I don't understand what the route in WebApiConfig does.

Nothing special, really.

There's just this convention to put startup code in static class methods in the App_Start directory, and call those from the Global.asax.

This convention changed ever so slightly with the MVC 5 / WebAPI 2 release, so if you follow a somewhat outdated tutorial, or one that's for the newer version while using the older, or upgrade the MVC version in your project, or add WebAPI to an existing MVC project you might end up with duplicate or wrong code. Be sure to follow the upgrade path if you do upgrade.

You seem to have done something like described above, given you have both WebApiConfig.Register(GlobalConfiguration.Configuration) and RouteConfig.RegisterRoutes(RouteTable.Routes).

In the end, that will cause this code:

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

To be called twice, causing the error you see.



来源:https://stackoverflow.com/questions/33102079/a-route-named-defaultapi-is-already-in-the-route-collection-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!