路由是在我的区域中找到控制器,但不是视图

发布于 2024-09-14 10:26:14 字数 1296 浏览 7 评论 0原文

我正在尝试使用 Maarten Balliauw 的 域路由< /a> 类将子域映射到 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 仅在根视图文件夹中查找视图,而不是区域内的视图文件夹。如果我将视图从区域的视图文件夹复制到根视图文件夹,页面将呈现良好。但这完全违背了APP划分Area的初衷。

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

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 }
        ));
    }
}

我很困惑为什么它可以在区域内找到控制器,而不是视图。

I'm trying to use Maarten Balliauw's Domain Route class to map sub-domains to the areas in an MVC2 app so that I have URLs like:

http://admin.mydomain.com/home/index

instead of:

http://mydomain.com/admin/home/index

So far, I've only had partial success. Execution is being routed to the correct controller in the correct area, but it cannot then find the correct view. I'm receiving the following error:

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 

This indicates to me that MVC is looking for the view only in the root views folder and not the views folder within the Area. If I copy the view from the Area's views folder to the root views folder, the page renders fine. This however, completely defeats the purpose of dividing the APP into Areas.

I'm defining the route for the area as:

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 }
        ));
    }
}

I'm confused as to why it is finding the controller within the Area fine, but not the view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め七分饶幸 2024-09-21 10:26:14

好吧,我明白了。下载 MVC 2 源代码并将其添加到我的解决方案后,如下所述 此处,我单步调试了 MVC 代码。我发现区域内的路由实现了 IRouteWithArea 接口。该接口向 RouteData 添加了一个“Area”属性,毫不奇怪,该属性包含区域的名称。我修改了 DomainRoute 类来实现此接口,并添加了几个采用此附加参数的重载构造函数,它现在完全按照我想要的方式工作。

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

 context.Routes.Add(
     "Admin_Default"
     , new DomainRoute(
         "admin.mydomain"
         ,"Admin"
         , "{controller}/{action}/{id}"
         , new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
        ));

OK, I figured it out. After downloading the MVC 2 source code and adding it to my solution as outlined here, I stepped through the MVC code. I found that Routes within areas implement the IRouteWithArea interface. This interface adds an 'Area' property to the RouteData which, not surprisingly, contains the area's name. I modified the DomainRoute class so to implement this interface and added a couple of overloaded constructors that took this additional parameter, and it now works exactly as I wanted it to.

The code for registering my route now looks like this:

 context.Routes.Add(
     "Admin_Default"
     , new DomainRoute(
         "admin.mydomain"
         ,"Admin"
         , "{controller}/{action}/{id}"
         , new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
        ));
青衫负雪 2024-09-21 10:26:14

如果您的区域和默认路由之间共享控制器名称(而且看起来确实如此),那么您可能需要在调用 MapRoute 时识别命名空间。

例如,如果您的 Web 应用程序的顶级命名空间是 Web,则 Global.asax.cs 文件中的 RegisterRoutes 方法将看起来像这样像这样:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",        
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Controllers" }
);

然后 AdminAreaRegistration.csRegisterArea 方法看起来像这样:

context.MapRoute(
    "Admin_Default",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Areas.Admin.Controllers" }
);

If you have share controller names between your areas and your default routes, and it looks like you do, you may need to identify namespaces when you call MapRoute.

For example, if the top-level namespace of your web application is Web, the RegisterRoutes method in Global.asax.cs file would look something like this:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",        
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Controllers" }
);

and then the RegisterArea moethod of AdminAreaRegistration.cs would look something like this:

context.MapRoute(
    "Admin_Default",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    null,
    new string[] { "Web.Areas.Admin.Controllers" }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文