路线收集错误中已存在名为"DefaultApi"的路线 [英] A route named 'DefaultApi' is already in the route collection error

查看:43
本文介绍了路线收集错误中已存在名为"DefaultApi"的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的代码:

RouteConfig.cs:

 使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Web;使用System.Web.Mvc;使用System.Web.Routing;使用System.Web.Http;命名空间已审核{公共类RouteConfig{公共静态无效RegisterRoutes(RouteCollection路由){route.IgnoreRoute("{resource} .axd/{* pathInfo}");route.MapHttpRoute(名称:"GetReviewComments",routeTemplate:"api/reviews/comments/{id}",默认值:新{id = RouteParameter.可选,控制器=评论",操作=评论"});route.MapHttpRoute(名称:"GetByCategories",routeTemplate:"api/reviews/categories/{category}",默认值:new {category = RouteParameter.可选,控制器="Reviews",action ="GetByCategory"});route.MapHttpRoute(名称:"DefaultApi",routeTemplate:"api/{controller}/{id}",默认值:new {id = RouteParameter.Optional});route.MapRoute(名称:默认",url:"{controller}/{action}/{id}",默认值:new {controller ="Home",action ="Index",id = UrlParameter.Optional});}}} 

Global.asax:

 使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Web;使用System.Web.Http;使用System.Web.Mvc;使用System.Web.Optimization;使用System.Web.Routing;使用System.Data.Entity;使用Reviewed.Models;命名空间已审核{//注意:有关启用IIS6或IIS7经典模式的说明,//访问http://go.microsoft.com/?LinkId=9394801公共类WebApiApplication:System.Web.HttpApplication{受保护的void Application_Start(){AreaRegistration.RegisterAllAreas();WebApiConfig.Register(GlobalConfiguration.Configuration);FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles);Database.SetInitializer(new DrawnContextInitializer());}}} 

解决方案

我不了解WebApiConfig中的路由是什么.

没什么特别的.

只有一种约定,将启动代码放在App_Start目录中的静态类方法中,然后从Global.asax中调用这些代码.

在MVC 5/WebAPI 2发行版中,此约定发生了很小的变化,因此,如果您遵循的教程有些过时,或者在使用旧版本时使用的是新版本,或者在项目中升级MVC版本,或者添加WebAPI到现有MVC项目时,您可能会得到重复或错误的代码.确保遵循升级路径(如果要升级).

假设您同时具有 WebApiConfig.Register(GlobalConfiguration.Configuration) RouteConfig.RegisterRoutes(RouteTable.Routes),您似乎已经完成了上述操作./p>

最后,这将导致以下代码:

  GlobalConfiguration.Configuration.Routes.MapHttpRoute(名称:"DefaultApi",routeTemplate:"api/{controller}/{id}",默认值:new {id = RouteParameter.Optional}); 

要被调用两次,导致您看到错误.

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

解决方案

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.

这篇关于路线收集错误中已存在名为"DefaultApi"的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆