문제

나는 읽었다 이것 Ruby on Rails에서 경로를 접두사 할 수있는 방법에 대한 기사. asp.net mvc와 똑같은 일을 할 수 있기를 원합니다.

그래서 나는 다음과 같은 경로를 정의 할 수 있기를 원합니다.

/photographers/1/photos/2   //photo 2 of photographer with id 1
/photographers/1/photos     //all of photographer with id 1

팁이 있습니까?

편집하다:

"사진가/{id}/photos/{photoid}" - 일을 잘하는 것 같지만 어떻게 지원할 수 있습니까?

RedirectToAction<PhotosController>(x => x.Add());

:/photogrys/1/photos/add로 리디렉션하고 싶습니다.

도움이 되었습니까?

해결책

다음과 같이 경로를 정의하십시오.

routes.MapRoute(
    "Photographers",
    "photographers/{id}/photos/{photoID}",
    new { controller = "Photographers", action = "Photo", photoID = null });

그런 다음 다음과 같은 컨트롤러 작업을 정의하십시오.

public ActionResult Photo(int id, int? photoID)
{
    // If photoID is not null, show just that photo.
    // Otherwise, show all photographs.
}

다른 팁

당신은 사용할 수 있습니다 정규식 라우팅 또는 사용 라우팅 테이블의 와일드 카드 {id*}가 일치하도록합니다 /1/photos/2 사진 작가 기본 컨트롤러의 경우 문자열을 구문 분석하고 적절한 작업으로 리디렉션하십시오.

또한 살펴보십시오 이것 중첩 된 자원에 대해 게시하십시오.

RouteTable.Routes.Add(
        new Route { Url = "events/[eventId]/tickets/[action]/[id]", 
                    Defaults = new { controller = "Tickets", 
                                     action = "List", id = (string)null }, 
                    RouteHandler = typeof(MvcRouteHandler) });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top