Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 具有多个区域的ASP.NET MVC中的路由_Asp.net Mvc_Asp.net Mvc 4_Routes - Fatal编程技术网

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

Asp.net mvc 具有多个区域的ASP.NET MVC中的路由,asp.net-mvc,asp.net-mvc-4,routes,Asp.net Mvc,Asp.net Mvc 4,Routes,我在ASP.NET MVC Web应用程序中有两个不同的领域。 1.管理员 2.场地 我想要我的应用程序到站点区域的默认路径 i、 e www.mydomain.com/Site/Home 为此,我将默认路由配置如下。 但这种配置对我不起作用 现场区域路线如下所示 管理区路径如下所示 非常感谢你的帮助 我想你搞错了控制器名称空间 您的主要路线定义应为: routes.MapRoute( name: "Default", url: "{controller}/{action}/{i

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

我想要我的应用程序到站点区域的默认路径 i、 e www.mydomain.com/Site/Home

为此,我将默认路由配置如下。 但这种配置对我不起作用

现场区域路线如下所示

管理区路径如下所示

非常感谢你的帮助


我想你搞错了控制器名称空间

您的主要路线定义应为:

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" }
);
您的区域路径(如管理区域注册)应为:

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" }
);
执行以下步骤:

  • 创建项目:项目类型:Framework 4.6.1>WebApi和MVC Web
  • 项目布局将具有[区域文件夹]
  • 在[Areas folder]中可以找到文件夹[HelpPage folder]
  • 右键单击>>[区域文件夹]选择区域>“为其命名:管理”
  • Visual Studio为您注册所有内容。 区域

    管理

    控制器 模型 意见 AdministrationarRegistration.cs

  • 创建一个控制器>读写:给它一个名称[Admin]和[View]索引

  • 然后在布局中添加链接:在普通主页链接下
  • @ActionLink(“Home”,“Index”,“Home”,new{area=”“},null)
  • @ActionLink(“Admin”,“Index”,“Admin”,new{area=“Administration”},null)
  • 单击链接并查看您的管理区域

  • 主文件夹(MyApp.Controllers)中没有任何控制器。我想将站点区域“MyApp.Areas.Site.Controllers”作为默认路由。因此,您应该将RegisterRoutes中的Url更改为:“Site/{controller}/{action}/{id}”,请首先在回答任何问题之前,您应该实际尝试一下,这样会浪费您和其他人的时间。。。我已经尝试了所有可能的场景:)
    公共类管理注册:AreaRegistration{public override string AreaName{get{return“Administration”;}}public override void RegisterArea(AreaRegistrationContext){context.MapRoute(“Administration_default”,“Administration/{controller}/{action}/{id}”,new{action=“Index”,id=UrlParameter.Optional});}}
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new { controller = "Home" },
        new[] { "MyApp.Areas.Admin.Controllers" }
    );