한 가지 방법을 각 방문 페이지의 경로 또 다른 방법은 각 방문 페이지와 일치하는 제약 조건이있는 단일 경로를 만드는 것입니다.
routes.MapRoute(
"LandingPage1"
"landingpage1/{id}",
new { controller = "home", action = "landingpage", id = UrlParameter.Optional });
routes.MapRoute(
"LandingPage2"
"landingpage2/{id}",
new { controller = "home", action = "landingpage2", id = UrlParameter.Optional });
약간의 리플렉션 (검증 안 함)으로이 작업을 수행 할 수 있습니다.
foreach (var method on typeof(HomeController).GetMethods())
{
if (method.ReturnType.IsInstanceOf(typeof(ActionResult)))
{
routes.MapRoute(
method.Name,
method.Name + "/{id}",
new { controller = "home", action = method.Name, id = UrlParameter.Optional });
}
}
RouteConstraint 솔루션은 적절한 경로 값이 만약 그렇다면, HomeController의 방법 중 하나를 일치 여부 평가 사용자 정의 제약 조건 단일 경로를 줄 것을 제외하고는 유사하다, 컨트롤러를 교체 "집"과 일치하는 값을 가진 행동.
routes.MapRoute(
"LandingPage",
"{action}/{id}",
new { controller = "home", action = "index", id = UrlParameter.Optional },
new LandingPageRouteConstraint()
);
public LandingPageRouteContstraint : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
// simplistic, you'd also likely need to check that it has the correct return
// type, ...
return typeof(HomeController).GetMethod(values.Values["action"]) != null;
}
}
리플렉션을 사용하는 경우에도 페이지 당 경로 메커니즘은 한 번만 수행됩니다. 그때부터 당신은 매번 간단한 조회를합니다. RouteConstraint 메커니즘은 매번 리플렉션을 사용하여 경로가 일치하는지 확인합니다 (결과를 캐시하지 않는 한 생각하지 않습니다).
완벽 함, 덕분에 도움이됩니다. 감사! –