2015-10-13 67 views
1

儘管在堆棧溢出中嘗試了不同的解決方案,但我無法解決此問題。我曾嘗試過:名爲'DefaultApi'的路由已經在路由收集錯誤中

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

的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()); 
     } 
    } 
} 
+3

只需搜索字符串「DefaultApi」的解決方案即可。我很確定另一個在'WebApiConfig.Register()'中註冊。 – CodeCaster

+0

謝謝你的幫助。你可以添加它作爲答案。但我不明白WebApiConfig中的路線是什麼。 – tabby

回答

0

我不明白在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 } 
); 

被稱爲兩次,導致你看到的錯誤。