如何在带有区域的asp.net mvc中配置路由

时间:2012-10-25 12:22:38

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

我正在使用asp.net mvc 3.我的项目中有三个区域,如

MyProject/Areas/Blogs
MyProject/Areas/Forums
MyProject/Areas/Groups

在这三者中,博客视图是启动视图。因为我已将globla.ascx设置为

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Blog", action = "Blog", id = UrlParameter.Optional }
            );

并在BlogAreaRegistration.cs中,

context.MapRoute(
                "Blogarea_Default",
                "{controller}/{action}/{id}",
                new { controller = "Blog", action = "Blog", id = UrlParameter.Optional }
            );

并在ForumAreaRegistration.cs中,

context.MapRoute(
                null,
                "Forums/{action}/{id}",
                new {controller="Forums", action = "Forum", id = UrlParameter.Optional }
            );

并在GroupsAreaRegisration.cs中,

   context.MapRoute(
        "Groups_default",
        "Groups/{controller}/{action}/{id}",
        new { controller = "Groups", action = "Group", id = UrlParameter.Optional }
    );

此处论坛和博客正如我所愿,但该集团无法正常工作它始终显示 404资源未找到页面所以如果我在此过程中有任何错误,请指导我。

2 个答案:

答案 0 :(得分:2)

尝试更改

 context.MapRoute(
        "Groups_default",
        "Groups/{controller}/{action}/{id}",
        new { controller = "Groups", action = "Group", id = UrlParameter.Optional }
    );

有:

 context.MapRoute(
        "Groups_default",
        "Groups/{action}/{id}",
        new { controller = "Groups", action = "Group", id = UrlParameter.Optional }
    );

答案 1 :(得分:0)

您应该从Groups/代码块中删除{controller}/GroupsAreaRegisration.cs - 然后它应该有效。我删除了{controller}/,因此代码将为:

context.MapRoute(
    "Groups_default",
    "Groups/{action}/{id}",
    new { controller = "Groups", action = "Group", id = UrlParameter.Optional }
);

同样在BlogAreaRegistration.cs我将{controller}替换为Blog,否则您可能会得到意想不到的结果。这里的完整代码将是

context.MapRoute(
    "Blogarea_Default",
    "Blog/{action}/{id}",
    new { controller = "Blog", action = "Blog", id = UrlParameter.Optional }
);