【问题标题】:Area routing in ASP.NET MVCASP.NET MVC 中的区域路由
【发布时间】:2014-07-29 18:41:55
【问题描述】:

我对区域路由有点困惑。我创建了一个名为 Backbone 的区域。我也有我的默认控制器、视图和模型。

http://localhost:46870/ 

给我以下错误:

Multiple types were found that match the controller named 'home'. This can happen if     
the route that services this request ('{controller}/{action}/{id}') does not specify   
namespaces to search for a controller that matches the request. If this is the case,  
register this route by calling an overload of the 'MapRoute' method that takes a 
'namespaces' parameter.

The request for 'home' has found the following matching controllers:
LearnJavascript.Areas.BackBone.Controllers.HomeController
LearnJavascript.Controllers.HomeController

这里是主干路由(这个是脚手架自带的,我没做任何改动):

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

默认路由(这个是脚手架自带的,我没做任何改动):

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

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

我在想,除非网址以

开头
http://localhost:46870/Backbone

骨干区域 home 不会被调用。那么,为什么路由会对此感到困惑。

最令人困惑的部分是当我调用这个网址时:

http://localhost:46870/home/index

它向我显示了相同的错误消息。为什么 MVC 路由对此如此困惑。

我正在使用 VS2013 和 MVC5。

【问题讨论】:

  • 即使在不同的区域,你也不能拥有两个同名的控制器。
  • 我认为拥有相同的控制器名称是拥有一个区域的主要目的。
  • 请检查[找到与名为'Home'的控制器匹配的多种类型][1] [1]:stackoverflow.com/questions/7842293/…
  • @HamidBahmanabady 我将命名空间添加到全局路由,它开始正常工作。谢谢。
  • @MattBodily 我们可以在区域中使用相同的控制器名称。这是拥有区域的主要目的之一。我刚刚创建了另一个区域并仔细检查了它。 :)

标签: asp.net-mvc asp.net-routing


【解决方案1】:

我从 HamidBahmanabady 那里得到了正确答案。

我将命名空间添加到全局路由并开始正常工作。

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

        context.MapRoute(
            "BackBone_default",
            "BackBone/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            new[] { "Test.Areas.Backbone.Controllers" }

【讨论】:

  • 嗨,我已经尝试过你的代码,但它再次出现错误“健康”的请求找到了以下匹配的控制器:Appname.Areas.Admin.Controllers.HealthController Appname.Controllers.HealthController , 我该怎么办?
【解决方案2】:

试试这个,

在 route.config 中

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

和管理部分 --> AdminAreaRegistration.cs

  context.MapRoute(
        "BackBone_default",
        "BackBone/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new string[] { "Test.Areas.Backbone.Controllers" }

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 2010-12-09
    • 2018-05-31
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多