名为"x"的路径已在路径集合中.路线名称必须是唯一的.ASP.NET MVC 3的例外

Rn2*_*222 95 asp.net asp.net-mvc-3-areas asp.net-mvc-3

我正在做一个ASP.NET MVC 3 Web服务,我一直在间歇性地得到这个例外.

堆栈跟踪:

Server Error in '/' Application.

A route named 'ListTables' is already in the route collection. Route names must be unique.
Parameter name: name

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: A route named 'ListTables' is already in the route collection. Route names must be unique.
Parameter name: name

Source Error: 


Line 24:            //     }
Line 25:            // );
Line 26:             context.MapRoute(
Line 27:                 "ListTables",
Line 28:                 // example: 

Source File: C:\inetpub\wwwroot\SchemaBrowserService\Website\Areas\Api\ApiAreaRegistration.cs    Line: 26 

Stack Trace: 


[ArgumentException: A route named 'ListTables' is already in the route collection. Route names must be unique.
Parameter name: name]
   System.Web.Routing.RouteCollection.Add(String name, RouteBase item) +2329682
   System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults, Object constraints, String[] namespaces) +236
   System.Web.Mvc.AreaRegistrationContext.MapRoute(String name, String url, Object defaults, Object constraints, String[] namespaces) +59
   System.Web.Mvc.AreaRegistrationContext.MapRoute(String name, String url, Object defaults) +17
   SchemaBrowserService.Areas.Api.ApiAreaRegistration.RegisterArea(AreaRegistrationContext context) in C:\inetpub\wwwroot\SchemaBrowserService\Website\Areas\Api\ApiAreaRegistration.cs:26
   System.Web.Mvc.AreaRegistration.CreateContextAndRegister(RouteCollection routes, Object state) +105
   System.Web.Mvc.AreaRegistration.RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, Object state) +199
   System.Web.Mvc.AreaRegistration.RegisterAllAreas(Object state) +45
   System.Web.Mvc.AreaRegistration.RegisterAllAreas() +6
   Website.MvcApplication.Application_Start() in C:\Users\djackson\Downloads\RestApiMvc3\Website\Website\Global.asax.cs:35

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
Run Code Online (Sandbox Code Playgroud)

这可能与路由调试器显示我有一些旧路由已被修改或删除并且不会消失(甚至在重新启动我的机器后)有关.堆栈跟踪还指一个源文件,该文件早已被删除,我的应用程序已移动到新位置,从那时起清理并重建.我错过了什么?

这是我的所有路线注册码:

// in Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Default2", // Route name
        "Api/{controller}/{action}/{id}", // URL with parameters
        new { controller = "DataSource", action = "Index", area = "Api", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
}

// in ApiAreaRegistration.cs:
public class ApiAreaRegistration : AreaRegistration
{
    public override string AreaName { get { return "Api"; } }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        // DataSources

        // Tables
        context.MapRoute(
            "ListTables",
            // example: 
            // /api/DataSources/DataSource/1/schemata/schema/dbo/tables
               "Api/DataSources/DataSource/{dataSourceId}/schemata/{schemaName}/tables",
            new
            {
                controller = "Tables",
                action = "TableList",
                schemaName = "dbo",
                dataSourceId = "DefaultId"
            }
        );


        // Schemata
        context.MapRoute(
          "Schema",
            // example: 
            // /api/DataSources/DataSource/1/schemata/schema/dbo
              "Api/DataSources/DataSource/{dataSourceId}/schemata/{schemaName}",
          new
          {
              controller = "Schema",
              action = "Schema",
              dataSourceId = "DefaultId",
              schemaName = UrlParameter.Optional
          }
       );

       // // DataSources
        context.MapRoute(
            "SingleDataSource",
            "Api/DataSources/DataSource/{dataSourceId}",
            new
            {
                controller = "DataSource",
                action = "DataSource",
                dataSourceId = UrlParameter.Optional
            }
        );
        context.MapRoute(
            "ListDataSources",
            "Api/DataSources",
            new
            {
                controller = "DataSource",
                action = "DataSourceList",
                dataSourceId = "DefaultId"
            }
        );
        context.MapRoute(
             "Api_default",
             "Api/{controller}/{action}/{id}",
             new { action = "Index", id = UrlParameter.Optional }
        );

    }
}
Run Code Online (Sandbox Code Playgroud)

Fle*_*lea 283

要解决这个问题,我必须进入项目的bin文件夹,删除所有DLL文件,然后重建,这解决了问题.

  • @Bomboca - 清除不会删除不属于项目的DLL.例如,如果您更改了项目的程序集名称,则旧程序集将保留在`bin`文件夹中. (83认同)
  • 只需清洁解决方案,我们就会做同样的事情. (10认同)
  • 我遇到了同样的问题,这立即解决了我的问题.谢谢! (2认同)
  • 我不知道为什么,但"清洁解决方案"对我不起作用.你的解决方案有效 (2认同)

Max*_*Max 19

由于多种原因可能会发生此错误,我遇到了相同的错误,并通过修改Global.asax类解决了这个问题.

Global.asax.cs中的Application_Start方法如下:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Run Code Online (Sandbox Code Playgroud)

以下行在此方法中出现两次:

RouteConfig.RegisterRoutes(RouteTable.Routes);
Run Code Online (Sandbox Code Playgroud)

这确保了路由被两次添加到路由列表并同时导致错误.

我按如下方式更改了Application_Start方法,错误消失了:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Run Code Online (Sandbox Code Playgroud)

这可能不是您的问题的答案,但可能在将来帮助其他人.我没有在其他人之间看到这个答案,所以我决定加上这个.

  • 还有对AreaRegistration.RegisterAllAreas()的重复调用. (2认同)

Rn2*_*222 9

在我重命名之前,我发现Global.asax指的是网站DLL文件的旧版本.当我执行Build> Clean up时,DLL没有被清理,因为VS项目/解决方案不再引用它了.似乎有时只使用较新版本的DLL,允许网站正常工作,但最终它们都会被加载导致路由冲突.

  • 你怎么知道Global.asax是指一个旧的DLL? (3认同)

Fil*_*p W 5

路由是从 AppDomain.CurrentDomain 中的所有程序集加载的,因此如果您的旧程序集仍然是其中的一部分,您可能仍在获得旧的/重复的路由。

  • 我怎样才能检查这是否是问题? (3认同)