我在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}
);
}
非常感谢您的帮助
答案 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)
执行以下步骤:
Visual Studio为您注册所有内容。 区域
管理
控制器 楷模 观看次数 AdministrationAreaRegistration.cs Filestructure example
创建一个控制器>读写:给它命名为[Admin]和[View]索引enter image description here