我的项目中有两个区域,Admin
和Client
。
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new { language = "en-US", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Testing.Controllers" }
);
}
目前的结果:
[✓] localhost
[✓] localhost/en-us
[✓] localhost/zh-hk
[✗] localhost/admin
[✗] localhost/client
我希望我可以这样做:
localhost - Home Page (Default Language)
localhost/en-us - Home Page (English)
localhost/zh-hk - Home Page (Traditional Chinese)
localhost/admin/en-us - Admin Area Home Page (English)
localhost/admin/zh-hk - Admin Area Home Page (Traditional Chinese)
localhost/client/en-us - Client Area Home Page (English)
localhost/client/zh-hk - Client Area Home Page (Traditional Chinese)
您需要在通用路由注册之前注册区域
在通用路线之前添加AreaRegistration.RegisterAllAreas();
,
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new { language = "en-US", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Testing.Controllers" }
);
}
您的管理区域注册,
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{language}/{controller}/{action}/{id}",
new { language = "en-US", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
客户区注册,
public class ClientAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Client";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Client_default",
"Client/{language}/{controller}/{action}/{id}",
new { language = "en-US", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}