我使此解決方案:
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