名为“x"的路由已经在路线集合中.路由名称必须是唯一的.ASP.NET MVC 3 的异常 [英] A route named "x" is already in the route collection. Route names must be unique. Exception with ASP.NET MVC 3

查看:12
本文介绍了名为“x"的路由已经在路线集合中.路由名称必须是唯一的.ASP.NET MVC 3 的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个 ASP.NET MVC 3 Web 服务,但我一直间歇性地收到这个异常.

I'm doing an ASP.NET MVC 3 web service and I keep getting this exception intermittently.

堆栈跟踪:

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:inetpubwwwrootSchemaBrowserServiceWebsiteAreasApiApiAreaRegistration.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:inetpubwwwrootSchemaBrowserServiceWebsiteAreasApiApiAreaRegistration.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:UsersdjacksonDownloadsRestApiMvc3WebsiteWebsiteGlobal.asax.cs:35

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

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

It's probably related to the fact that Route Debugger shows that I have some old routes that I've modified or deleted and won't go away (even after rebooting my machine). The stack trace also refers to a source file that has long since been deleted and my app has been moved to a new location, cleaned and rebuilt since then. What am I missing?

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

Here is all of my route registration code:

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

    }
}

推荐答案

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

To fix this problem I had to go into the bin folder on my project, delete all DLL files and then rebuild and this fixed the problem.

这篇关于名为“x"的路由已经在路线集合中.路由名称必须是唯一的.ASP.NET MVC 3 的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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