Asp.net mvc 路由是在我的区域中查找控制器,而不是视图

Asp.net mvc 路由是在我的区域中查找控制器,而不是视图,asp.net-mvc,asp.net-mvc-2,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mvc Routing,我试图使用Maarten Balliauw的类将子域映射到MVC2应用程序中的区域,这样我就有了如下URL: http://admin.mydomain.com/home/index 而不是: http://mydomain.com/admin/home/index 到目前为止,我只取得了部分成功。正在将执行路由到正确区域中的正确控制器,但它无法找到正确的视图。我收到以下错误: The view 'Index' or its master was not found. The followi

我试图使用Maarten Balliauw的类将子域映射到MVC2应用程序中的区域,这样我就有了如下URL:

http://admin.mydomain.com/home/index
而不是:

http://mydomain.com/admin/home/index
到目前为止,我只取得了部分成功。正在将执行路由到正确区域中的正确控制器,但它无法找到正确的视图。我收到以下错误:

The view 'Index' or its master was not found. The following locations were searched:
~/Views/AdminHome/Index.aspx
~/Views/AdminHome/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx 
这向我表明MVC只在根视图文件夹中查找视图,而不是该区域内的视图文件夹。如果将视图从区域的“视图”文件夹复制到“根视图”文件夹,则页面呈现良好。然而,这完全违背了将应用程序划分为多个区域的目的

我将该区域的路线定义为:

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get { return "Admin"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.Routes.Add(
            "Admin_Default"
            , new DomainRoute(
                "admin.localhost"
                , "{controller}/{action}/{id}"
                , new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
        ));
    }
}

我不明白为什么它可以在区域内找到控制器,但不能在视图中找到控制器。

如果您的区域和默认路由之间有共享控制器名称,并且看起来是这样,那么在调用
MapRoute
时,您可能需要标识名称空间

例如,如果web应用程序的顶级名称空间是
web
,则
Global.asax.cs
文件中的
RegisterRoutes
方法如下所示:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",        
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Controllers" }
);
context.MapRoute(
    "Admin_Default",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Areas.Admin.Controllers" }
);
 context.Routes.Add(
     "Admin_Default"
     , new DomainRoute(
         "admin.mydomain"
         ,"Admin"
         , "{controller}/{action}/{id}"
         , new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
        ));
然后,
AdminAreaRegistration.cs
RegisterArea
方法将如下所示:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",        
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Controllers" }
);
context.MapRoute(
    "Admin_Default",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Areas.Admin.Controllers" }
);
 context.Routes.Add(
     "Admin_Default"
     , new DomainRoute(
         "admin.mydomain"
         ,"Admin"
         , "{controller}/{action}/{id}"
         , new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
        ));

好的,我知道了。在下载MVC2源代码并将其添加到我的解决方案中(如所述)之后,我逐步完成了MVC代码。我发现区域内的路由实现了IRouteWithArea接口。该接口向RouteData添加了一个“Area”属性,毫不奇怪,该属性包含区域的名称。为了实现这个接口,我修改了DomainRoute类,并添加了两个重载构造函数,它们接受了这个额外的参数,现在它完全按照我的要求工作

现在注册我的路线的代码如下所示:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",        
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Controllers" }
);
context.MapRoute(
    "Admin_Default",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Areas.Admin.Controllers" }
);
 context.Routes.Add(
     "Admin_Default"
     , new DomainRoute(
         "admin.mydomain"
         ,"Admin"
         , "{controller}/{action}/{id}"
         , new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
        ));

添加多条路线怎么样?如果您需要的路由不仅仅是管理默认路由,该怎么办?比如,如果您希望其他控制器的路由和管理区域中的其他操作怎么办?我还没有看到任何人展示这方面的例子……只是展示了如何处理区域的默认路线指向的例子。+1这让我困惑了相当一段时间。一旦我发现正确的控制器在该地区被击中,但它提供了错误的看法,我很快找到你的问题和答案-工作很好。