Area中AdminAreaRegistration如下:
1 public class AdminAreaRegistration : AreaRegistration
2 {
3 public override string AreaName
4 {
5 get
6 {
7 return "Admin";
8 }
9 }
10
11 public override void RegisterArea(AreaRegistrationContext context)
12 {
13 context.MapRoute(
14 "Admin_default",
15 "Admin/{controller}/{action}/{id}",
16 new { controller="Manage", action = "Index", id = UrlParameter.Optional }
17 );
18 }
19 }
Global.asax中如下設置:
1 public static void RegisterRoutes(RouteCollection routes)
2 {
3 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
4
5 routes.MapRoute(
6 "Default", // 路由名稱
7 "{controller}/{action}/{id}", // 帶有參數的 URL
8 new { controller = "Manage", action = "Index", id = UrlParameter.Optional } // 參數默認值
9 );
10
11 }
12
13 protected void Application_Start()
14 {
15 AreaRegistration.RegisterAllAreas();
16
17 RegisterGlobalFilters(GlobalFilters.Filters);
18 RegisterRoutes(RouteTable.Routes);
19 }
運行時提示
未找到視圖“Index”或其母版視圖,或沒有視圖引擎支持搜索的位置。搜索了以下位置:
~/Views/Manage/Index.aspx
~/Views/Manage/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Manage/Index.cshtml
~/Views/Manage/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
解決方式:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional},
namespaces:new string[]{"MVCUI.Areas.自己Area的名稱.Controllers"}
).DataTokens.Add("Area","自己Area的名稱");(就是Areas下一級的名稱)
}

