1

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());
        }
    }
}
CodeCaster
  • 131,656
  • 19
  • 190
  • 236
refresh
  • 1,203
  • 1
  • 17
  • 50
  • 3
    Just search through your solution for the string "DefaultApi". I'm pretty sure the other one is registered in `WebApiConfig.Register()`. – CodeCaster Oct 13 '15 at 12:02
  • Thank you for your help. You can add it as answer. But I don't understand what the route in WebApiConfig does. – refresh Oct 14 '15 at 05:10

1 Answers1

0

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.

Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236