【问题标题】:How to configure routes in asp.net mvc with Areas如何在 asp.net mvc 中使用 Areas 配置路由
【发布时间】:2012-10-25 12:22:37
【问题描述】:

我正在研究 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 找不到资源页面,所以如果我在此过程中犯了任何错误,请指导我。

【问题讨论】:

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


    【解决方案1】:

    尝试改变

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

    【讨论】:

    • @Rogulski:我做了上面提到的更改,但没有得到想要的输出。它再次显示相同的 404 错误
    • 生成 404 的 url 看起来如何?
    【解决方案2】:

    您应该从 GroupsAreaRegisration.cs 代码块中删除 Groups/{controller}/ - 然后它应该可以工作。我会删除{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 }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-12
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      相关资源
      最近更新 更多