路径已经在区域路径的路径集合中

时间:2013-10-09 05:34:41

标签: asp.net-mvc-4

这是我的mvc项目的主要路由表:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        AreaRegistration.RegisterAllAreas();
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    }
}

我有一个名为Docs的区域,这是注册:

public class DocsAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get{ return "Docs";}
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute("Docs_default", "Docs/{controller}/{action}", new { controller = "Wiki", action = "Index" });


        context.MapRoute("RESTApi", "wiki/restAPI/v1", new { controller = "Wiki", action = "RestAPI" });


        context.MapRoute("RESTApi", "wiki/test", new { controller = "Wiki", action = "action1" });
        context.MapRoute("RESTApi_test0", "wiki/test0", new { controller = "Wiki", action = "action2" });
        context.MapRoute("RESTApi_test1", "wiki/test2", new { controller = "Wiki", action = "productions" });
        context.MapRoute("RESTApi_test2", "wiki/test3", new { controller = "Wiki", action = "action3" });
        context.MapRoute("RESTApi_test3", "wiki/test4", new { controller = "Wiki", action = "action4" });
    }
}

我收到运行时错误:名为“Docs_default”的路径已经在路径集合中。路线名称必须是唯一的。

我不知道这里发生了什么冲突。或者我错过了什么?

(仅供参考,忽略行动的名称,它们仅用于测试目的......因此它们没有意义。)

1 个答案:

答案 0 :(得分:7)

您可能在Application_Start中调用了两次RegisterRoutes。

问题是重复的`AreaRegistration.RegisterAllAreas();在路线上和在global.asax

所以只需要这个:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

或执行以下步骤: -

检查您的bin文件夹。也许还有另一个.dll将相同的路由添加到RouteCollection。

当我重命名项目时,这发生在我身上。我的bin文件夹中有2个.dll:

MyProject.Web.dll
MyProjectNewName.Web.dll