路由集合中已存在名为 'Billing_Default' 的路由。路由名称必须是唯一的。参数名称:名称

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

删除了bin文件夹,也多次清理和重建。出于某种原因,在检查了配置路由的所有必需文件后,我 运行 进入了命名错误,我更改了它并对其进行了最大程度的操作,但我被卡住了。我正在努力学习asp.net,但是这个错误让我傻眼了。

这是文件夹系统,重要的我展开了:

Project-
   Reference-
   Packages-
   App_Start-
       -RouteConfig.cs
       -WebApiConfig.cs
   Areas-
       -Billing
       -Invoice
   Content-
   Controllers-
   Models-
   Scripts-
   Views-
   Global.asax
   Pacages.config
   Web.config

我的每个区域都有一个注册文件。

这是区域之一的注册码。

using System;
using System.Web.Mvc;

namespace WORK_GODDAMN.Areas.Billing
{

    public class BillingAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Billing";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Billing_default",
                "Billing/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }



}

这是我的 Global.asax 代码

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;

namespace WORK_GODDAMN
{
    public class Global : HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

这是我的路由配置代码

using System.Web.Mvc;
using System.Web.Routing;

namespace WORK_GODDAMN
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //AreaRegistration.RegisterAllAreas();

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "Core.Controllers" }
            );

        }
    }
}

应该重定向到预定义区域的索引页的 cshtml 代码。

<!DOCTYPE html>

<html>
    @using System.Web.Optimization
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Scripts.Render("~/Scripts/modernizr.js")
</head>
<body>
    <div>
        <div>
            <h2>{Demo}- Pluggable Modules</h2>
        </div>
        <div id="nav">
            @Html.ActionLink("Home","Index","Home", new {Area=""}, null) |
            @Html.ActionLink("Marketing","Index","Marketing", new {Area="Marketing"}, null) |
            @Html.ActionLink("BillingMain", "Index", new { Area = "Billing" })
        </div>
        <hr />
        <div>
                @RenderBody()
        </div>
    </div>
    @Scripts.Render("~/Scripts/jquery.js")
    @RenderSection("Scripts", required: false)
</body>
</html>

我不是为什么会发生这种冲突,我已经清理并重建了多次,我使用的是 2017 Mac Visual Studio 所以我不得不自己配置区域。

在此 ActionLink 中:

@Html.ActionLink("BillingMain", "Index", new { Area = "Billing" })

您没有指定控制器参数。

在您的区域注册中,您也没有指定控制器参数。

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Billing_default",
            "Billing/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

如果您没有在路由中指定控制器参数,则它是必需的参数,而不是可选参数。因此,此操作 link 将不会匹配您的区域路线,并将尝试匹配下一条已注册的路线(很可能是您的默认路线)。

要使控制器可选,您必须指定一个默认值。

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Billing_default",
            "Billing/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

否则,您必须在您的 ActionLink 中提供它以匹配:

@Html.ActionLink("Billing","Index","BillingMain", new {Area=""}, null)

路由值最合乎逻辑的行为通常是使值成为必需的(就像你有的那样),这样它们只有在 ActionLink 提供该值时才匹配。