C# 名为';计费默认值';已在路由集合中。路由名称必须是唯一的。参数名称:name
已删除bin文件夹,并已多次清理和重建。出于某种原因,在检查了配置路由的所有必需文件后,我遇到了命名错误,我对其进行了更改并将其控制到了最佳程度,但我被卡住了。我试图学习asp.net,但这个错误让我目瞪口呆 这是文件夹系统,我扩展了重要的文件夹系统:C# 名为';计费默认值';已在路由集合中。路由名称必须是唯一的。参数名称:name,c#,asp.net,asp.net-mvc,mef,C#,Asp.net,Asp.net Mvc,Mef,已删除bin文件夹,并已多次清理和重建。出于某种原因,在检查了配置路由的所有必需文件后,我遇到了命名错误,我对其进行了更改并将其控制到了最佳程度,但我被卡住了。我试图学习asp.net,但这个错误让我目瞪口呆 这是文件夹系统,我扩展了重要的文件夹系统: Project- Reference- Packages- App_Start- -RouteConfig.cs -WebApiConfig.cs Areas- -Billing
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);
}
}
}
这是我的routeConfig代码
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>
@使用System.Web.Optimization
@视图包。标题
@Scripts.Render(“~/Scripts/modernizer.js”)
{Demo}-可插拔模块
@ActionLink(“Home”,“Index”,“Home”,new{Area=”“},null)|
@ActionLink(“营销”、“索引”、“营销”,新的{Area=“Marketing”},空)|
@ActionLink(“BillingMain”,“Index”,new{Area=“Billing”})
@RenderBody()
@Scripts.Render(“~/Scripts/jquery.js”)
@RenderSection(“脚本”,必需:false)
我不知道为什么会发生这种冲突,我已经清理和重建了多次,我正在使用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 }
);
}
在区域注册中,也没有指定控制器参数
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Billing_default",
"Billing/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
如果未在路由中指定控制器参数,则它是必需参数,而不是可选参数。因此,此操作链接将不匹配您的区域路线,并将尝试匹配下一条注册的路线(很可能是您的默认路线)
要使控制器成为可选的,必须指定默认值
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提供值时匹配。您是否尝试移动
AreaRegistration.RegisteralAreas()
到Application_Start()
内部Global.asax
中注册部分的顶部?是的,我厌倦了各种组合。此外,您还需要删除AreaRegistration.Registeralareas()
来自RouteConfig
类,该类将尝试重新注册区域,并将导致您收到的错误类型。如果我这样做,则即使在将链接映射到一个按钮以将我带到那里之后,我也会收到一个not found 404,因此这些区域在开始时不会得到注册。我将添加我的html代码,我可能缺少一些SimpleB,但这是在“视图”文件夹中搜索,而不是在“区域/账单/视图”中搜索。我确实定义了控制器,但在尝试不同的操作时将其取出。我还添加了名称空间以指向正确的文件夹,但仍然出现相同的错误,其中显示“未找到视图‘Billing’或其主控项,或者没有视图引擎支持搜索的位置。搜索了以下位置:……”
添加名称空间不足以让MVC在其他位置查找视图。如果您打破了MVC的视图位置约定,那么您必须或者为了实现这一点。不过,这不是你的问题-这解决了你遇到的路由问题。它不是在区域内搜索吗?您正在使用哪个URL尝试访问它?您应该使用/Billing/BillingMain/Index
(假设BillingMainController
是您的控制器名称,Index
是您的操作名称,但您的问题也不清楚),它将到达您的区域路线,然后在正确的位置搜索您的视图。感谢您的帮助,我对网络开发非常陌生。我通过localhost:8080/BillingMain访问它,这给了我一个错误。是BillingMainController是我的计费控制器的名称。