具有多个区域的ASP.NET MVC中的路由

时间:2017-04-29 10:38:38

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

  

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

非常感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我认为您的控制器名称空间错误。

您的主要路线定义应为:

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

答案 1 :(得分:0)

执行以下步骤:

  1. 创建项目:项目类型:Framework 4.6.1> WebApi和MVC Web
  2. 项目布局将具有[区域文件夹]
  3. 在[Areas文件夹]内,您将找到[HelpPage文件夹]文件夹
  4. 右键单击>> [区域文件夹],选择区域>“给它命名:管理”
  5. Visual Studio为您注册所有内容。 区域

      

    管理

         
        

    控制器     楷模     观看次数     AdministrationAreaRegistration.cs     Filestructure example

      
  6. 创建一个控制器>读写:给它命名为[Admin]和[View]索引enter image description here

  7. 然后在您的布局中添加链接:在普通的首页链接下
  8. @ Html.ActionLink(“首页”,“索引”,“首页”,新的{area =“”},空)
  9. @ Html.ActionLink(“ Admin”,“ Index”,“ Admin”,新的{area =“ Administration”},空)
  10. 单击链接并查看您的管理区域