我使此解决方案:
AreaUtils.cs
using System;
using System.Web.Mvc;
using System.Web.Routing;
namespace SledgeHammer.Mvc.Site
{
public static class Utils
{
public static void RegisterArea<T>(RouteCollection routes,
object state) where T : AreaRegistration
{
AreaRegistration registration =
(AreaRegistration)Activator.CreateInstance(typeof(T));
AreaRegistrationContext context =
new AreaRegistrationContext(registration.AreaName, routes, state);
string tNamespace = registration.GetType().Namespace;
if (tNamespace != null)
{
context.Namespaces.Add(tNamespace + ".*");
}
registration.RegisterArea(context);
}
}
}
在global.asax中:
Utils.RegisterArea<SystemAreaRegistration>(RouteTable.Routes, null);
Utils.RegisterArea<ClientSitesAreaRegistration>(RouteTable.Routes, null);
//AreaRegistration.RegisterAllAreas(); do not dublicate register areas
否requred变化产生的区域的注册代码。 我还在路由中使用自定义常量来按请求(系统域或用户站点)中的域类型过滤路由。
这是我区注册为例:
namespace SledgeHammer.MVC.Site.Areas.System
{
public class SystemAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "System"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"System_Feedback",
"Feedback",
new { controller = "Feedback", action = "Index" }
);
context.MapRoute(
"System_Information",
"Information/{action}/{id}",
new { controller = "Information", action = "Index", id = UrlParameter.Optional }
);
}
}
}
namespace SledgeHammer.MVC.Site.Areas.ClientSites
{
public class ClientSitesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "ClientSites"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"ClientSites_default",
"{controller}/{action}/{id}",
new { controller = "Site", action = "Index", id = UrlParameter.Optional },
new { Host = new SiteInGroups("clients") }
);
}
}
}
嘿菲尔,感谢您的解释。 我最终将我的“站点”移出区域,现在它是“默认/非区域”部分(具有根和视图和控制器)。然后我将我的命名空间参数设置为默认控制器,以避免“模糊控制器”错误。 此外,我可能会设置约束,像^((?!管理)。*)忽略管理员在站点区域路线。 我同意,增加更多的复杂性并不是很好,尽管看起来mvc 1(非区域)路线可能潜在地严重依赖于订单。 谢谢! – Jason 2009-10-29 14:58:15
路由过程通常取决于规则顺序,所以'排序'功能可能很重要。 – twk 2009-11-03 22:56:15
请查看我对两种技术的回应,这两种技术可以让您按任何顺序订购区域注册(以及它们的路线)。 – Eilon 2010-01-21 07:40:43