2017-06-29 126 views
1

已删除的bin文件夹,也已清理并重建多次。出于某种原因,在检查完路由配置所需的所有文件后,我遇到了命名错误,我改变了它并将其尽可能地移植到了最大程度,但是我被卡住了。我正在尝试学习asp.net,但是这个错误让我目瞪口呆。名为'Billing_Default'的路由已经在路由集合中。路由名称必须是唯一的。参数名称:名称

这是系统文件夹,我已经扩大了重要的:

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> 

我不是为什么这个冲突正在发生的事情,我已经清理和重建多次,我使用的Mac 2017年的Visual Studio,所以我不得不来配置自己的领域。

+0

您是否尝试将'AreaRegistration.RegisterAllAreas();'移到'Global.asax'内'Application_Start()'的注册部分的顶部? –

+0

是的,我做了,我厌倦了各种各样的组合。 –

+1

此外,您还需要从'RouteConfig'类中移除'AreaRegistration.RegisterAllAreas();'这会尝试重新注册区域并导致您遇到的错误。 –

回答

1

在此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 { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 

否则,你必须在你的ActionLink提供它为它匹配:

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

为路径的值的最合乎逻辑的行为通常是让需要值(如你有),所以它们只有在ActionLink提供的值时才匹配。

+0

但这是在视图文件夹中搜索不在区域/帐单/视图。我确实已经定义了控制器,但是在尝试不同的事情时把它拿出来了。 –

+0

我还添加了命名空间指向正确的文件夹,仍然得到相同的错误,它说''查看'账单'或其主人没有找到或没有视图引擎支持搜索的位置。 .....“' –

+0

添加命名空间不足以使MVC在其他位置查看视图。如果您打破了MVC的视图位置约定,您必须[明确指定视图位置](https://stackoverflow.com/a/23335609/181087)或[修改ViewEngine中的搜索位置](https:// stackoverflow.com/a/909594/181087)以实现这一目标。不过,这不是你的问题 - 这解决了你遇到的路由问题。 – NightOwl888