2009-01-04 7 views
5

다음 시나리오를 처리하는 방법을 알아 내려고합니다. 일반적으로 테이블에 여러 레코드가 있습니다. 이들 모두는 트리를 형성하는 ID 및 ParentID 필드를 가지고 있습니다.ASP.NET MVC 페이지/서브 페이지 라우팅

Page1 
- Page2 
- Page3 
Page4 
- Page5 
-- Page6 

지금, 나는 페이지 3 및 Page6 내 경로가 respectivelly /Page1/Page6/Page3/Page5/Page6처럼되고 싶어요. 즉, 모든 부모를 URL에 포함하고자합니다.

컨트롤러 결과/라우팅을 설정하는 방법은 무엇입니까?

편집 : 위의 구조가 동적 될 것이라고 언급하는 것을 잊었다 - 등 노드를 추가 할 수 있습니다/삭제/변경 부모,

+0

[이]에서보세요 (http://stackoverflow.com/questions/296284/mvc-dynamic-routes) >> http://stackoverflow.com/questions/296284/mvc-dynamic- 노선 – Mark79

답변

3

당신은 와일드 카드 일치를 사용할 수 있습니다 참조 : http://www.vergentsoftware.com/blogs/ckinsman/ASPNETMVCWildcardRoutes.aspx

가능한 경로 :

routes.MapRoute("SomeName", "{*Page}", new { controller = "ControllerName", action = "ActionName" }); 

과 행동

문자열 페이지를 수용하고, 아마 분할로, 수동 구문 분석?

편집 :이 또한 유용 할 수 있습니다 : 컨트롤러 이름과 액션이 고정 URL을 기반으로하지 않는 것을 http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

+1

감사합니다, 많이 도움이되었습니다. 내 시간을 절약 했어. –

+0

@MichaelSamteladze np, 거의 8 년 전 :) – ccook

0

참고.

// place the more specific route first 
routes.MapRoute("r1", "{parent}/{child}/{subchild}", 
    new { controller = "Page", action = "Index", parent = parent, child = child, subchild = subchild }); 

routes.MapRoute("r2", "{parent}/{child}", 
    new { controller = "Page", action = "Index", parent = parent, child = child }); 


public class PageController 
{ 
    public ActionResult Index(string parent, string child, string? subchild) 
    { 
     // stuff 
    } 
}