MVC 2 AREAREGESTRATION ROUTES 순서
-
08-07-2019 - |
문제
MVC 2 Preview 2에서 AREAREGESTRATION이 각 영역의 경로를 임의의 순서로로드하고 있음을 알았습니다. 다른 쪽보다 먼저 얻을 수있는 좋은 방법이 있습니까?
예를 들어, "사이트"와 "admin"의 두 가지 영역이 있습니다. 둘 다 "블로그"컨트롤러가 있습니다.
다음을 원합니다.
/admin/ --> go to Admin's Blog controller
/ --> go to Site's Blog controller.
문제는 사이트의 경로를 먼저로드하고 있다는 것입니다. {controller}/{action}/{id}
대신에 admin/{controller}/{action}/{id}
URL "/admin/"로 이동하면 그런 다음 "사이트"영역에 관리자 컨트롤러가 없기 때문에 404를 얻습니다.
두 영역 모두 "블로그"컨트롤러로 기본적으로 기본적으로. 나는 단순히 넣을 수 있다는 것을 알고 있습니다 site/{controller}/...
URL로서, 그러나 가능하면 루트에 있으면됩니다. 또한 Global RegisterRoutes 기능에서 기본 경로를 유지하려고 시도했지만 "사이트"영역으로 전송되지 않습니다.
미리 감사드립니다!
해결책
현재 지역을 주문할 수 없습니다. 그러나 나는 각 영역을 가능한 한 다른 영역과 독립적으로 만들어서 주문을 중요하게 생각하지 않는 것이 합리적이라고 생각합니다.
예를 들어, default {컨트롤러}/{action}/{id} 경로가있는 대신 각 컨트롤러의 특정 경로로 교체 할 수 있습니다. 또는 해당 기본 경로에 제약 조건을 추가하십시오.
우리는 주문을 허용하기 위해 옵션을 넘어서고 있지만 기능을 과도하게 복제하고 싶지는 않습니다.
다른 팁
Haacked가 말한 것 외에도 지역 등록 (따라서 노선)을 주문할 수 있습니다. 원하는 순서대로 각 영역을 수동으로 등록하기 만하면됩니다. RegisterAllareas ()를 호출하는 것만 큼 매끄럽지는 않지만 확실히 가능합니다.
protected void Application_Start() {
var area1reg = new Area1AreaRegistration();
var area1context = new AreaRegistrationContext(area1reg.AreaName, RouteTable.Routes);
area1reg.RegisterArea(area1context);
var area2reg = new Area2AreaRegistration();
var area2context = new AreaRegistrationContext(area2reg.AreaName, RouteTable.Routes);
area2reg.RegisterArea(area2context);
var area3reg = new Area3AreaRegistration();
var area3context = new AreaRegistrationContext(area3reg.AreaName, RouteTable.Routes);
area3reg.RegisterArea(area3context);
}
또 다른 옵션은 RegisteralLareas ()에 대한 코드를 가져 와서 자신의 앱에 복사 한 다음 순서를 결정하기위한 자신의 메커니즘을 구축하는 것입니다. 내장 된 메소드가하는 모든 멋진 캐싱 로직을 원한다면 복사해야 할 코드가 약간 있지만 앱에는 필요하지 않을 수도 있습니다.
나는이 해결책을 만든다 :
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
생성 된 영역 등록 코드에 대한 변경 사항이 없습니다. 또한 요청의 도메인 유형 (시스템 도메인 또는 사용자 사이트)에 따라 경로를 필터링하기 위해 경로에서 사용자 정의 구조를 사용합니다.
이것은 예를 들어 내 지역 등록입니다.
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") }
);
}
}
}
참조를 위해
MVC3 (MVC2에 대해 알지 못함)에서 루트를 특정 영역/컨트롤러에 매핑하려면 전역 경로를 사용할 수 있습니다. 네임 스페이스/영역을 지정하는 것을 잊지 마십시오.
routes.MapRoute(
"CatchRoot", "",
new { controller = "SITEBLOG-CONTROLLER-NAME", action = "Index"}
).DataTokens.Add("area", "SITE-AREA-NAME");