【问题标题】:Routing in ASP.NET MVC with multiple areas具有多个区域的 ASP.NET MVC 中的路由
【发布时间】:2017-04-29 10:38:37
【问题描述】:

我在 ASP.NET MVC Web 应用程序中有两个不同的区域。 1. 管理员
2. 网站

我想让我的应用程序的默认路由到站点区域 即 www.mydomain.com/Site/Home

为此,我配置了默认路由,如下所示。 但是这个配置对我不起作用。

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 },
                namespaces: new string[] { "MyApp.Areas.Site.Controllers" }
            );
        }

地盘路线如下。

 public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                name: "Site_default",
                url: "Site/{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
            );
        }

行政区路线如下。

  public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                defaults: new { controller="Login",action = "Login", id = UrlParameter.Optional}
            );
        }

非常感谢您的帮助

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 routes


    【解决方案1】:

    我认为你的控制器命名空间有误。

    您的主要路线定义应该是:

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

    并且您的区域路线(如管理区域注册)应该是:

    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new { controller = "Home" },
        new[] { "MyApp.Areas.Admin.Controllers" }
    );
    

    【讨论】:

    • 我在主文件夹 (MyApp.Controllers) 中没有任何控制器。我想将站点区域“MyApp.Areas.Site.Controllers”设为默认路由。
    • 所以你应该把RegisterRoutes中的Url改成:"Site/{controller}/{action}/{id}"
    • 请您在回答任何问题之前先实际尝试一下,这样您就可以浪费您和其他人的时间...我已经尝试了所有可能的场景:)
    【解决方案2】:

    按照以下步骤进行:

    1. 创建项目:项目类型:Framework 4.6.1 > WebApi 和 MVC Web
    2. 项目布局将有 [区域文件夹]
    3. 在 [Areas 文件夹] 中,您会找到文件夹 [HelpPage 文件夹]
    4. 右击>>[区域文件夹]选择区域>“给它一个名字:管理”
    5. Visual Studio 为您注册一切。 领域

      管理

      控制器 楷模 意见 AdministrationAreaRegistration.cs Filestructure example

    6. 创建控制器 > 读写:给它一个名字 [Admin] 和 [View] Indexenter image description here

    7. 然后在您的布局中添加链接:在普通主页链接下
    8. @Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)
    9. @Html.ActionLink("Admin", "Index", "Admin", new { area = "Administration" }, null)
    10. 点击链接查看您的管理区域

    【讨论】:

    • public class AdministrationAreaRegistration : AreaRegistration { public override string AreaName { get { return "Administration"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Administration_default", "Administration/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2010-12-09
    • 2018-05-31
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多