2013-07-12 3 views
1

링크에 두 개 이상의 작업이 가능한지 궁금합니다. 예를 들어, 나는 다음과 같은 여러 링크하고 싶었 경우여러 작업 설정

http://www.mywebsite.com/(CONTROLLER)/(ID)/(ACTION)

[에 http : //] www.mywebsite.com/user/Micheal/EditMovies [HTTP : //] www.mywebsite. co.kr/user/Micheal/EditFavorites

이렇게하는 방법이 있습니까? 그렇지 않다면 함수에 여러 개의 ID를 지정하고 사례를 사용하여 전송할 페이지를 결정해야합니까? 내 UserController.cs에서

내가 가진 내 노선에서

public ActionResult Index(string username) 
    { 
     if (username != null) 
     { 
      try 
      { 
       var userid = (Membership.GetUser(username, false).ProviderUserKey); 
       Users user = entity.User.Find(userid); 
       return View(user); 
      } 
      catch (Exception e) 
      { 

      } 
     } 
     return RedirectToAction("", "Home"); 
    } 

를 내가 가지고 :

routes.MapRoute(
      name: "User", 
      url: "User/{username}", 
      defaults: new { controller = "User", action = "Index" } 
     ); 

내가 그것을 할 수 있도록 노력하고있어 나는 그래서 두 번째 작업을위한 추가 기능을 가지고있다 다음과 같이 할 수 있습니다 :

User/{username}/{actionsAdditional} 

그리고 내 UserController에서 다음과 같은 작업을 수행 할 수 있습니다. 현재,이 여러 가지 방법으로 할 수

public ActionResult Index(string username) 
    { 
     if (username != null) 
     { 
      try 
      { 
       var userid = (Membership.GetUser(username, false).ProviderUserKey); 
       Users user = entity.User.Find(userid); 
       return View(user); 
      } 
      catch (Exception e) 
      { 

      } 
     } 
     return RedirectToAction("", "Home"); 
    } 

public ActionResult EditFavorites() 
    { 

// DoStuff }

답변

0

actionsAdditional 두 번째 조치는 단지 하나 :

routes.MapRoute("UserEditsThings", 
    "user/{id}/edit/{thingToEdit}", 
    new { controller = "UserController", action="Edit" }, 
    new { thingToEdit = ValidThingsToEditConstraint() } 
); 

:

이 처리 할 수있는 경로를 설정 User 컨트롤러의 작업은 다음과 같아야합니다.

public ActionResult Edit(ThingToEdit thingToEdit) { 
    ThingToEditViewModel viewModel = new ThingToEditViewModel(thingToEdit); 
    return View(viewModel); 
} 

RouteConstraint은 입력합니다 (thingToEdit)을 타고 만들 것 무엇 그것은 (당신은 몇 곳에서이 작업을 수행 할 수 있습니다 - 같은 사용자 정의 ModelBinder를에) 유효 확인하십시오 것을,

public class ValidThingsToEditConstraint : IRouteConstraint 
{ 
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
    { 
     //simplistic implementation simply to show what's possible. 
     return values['thingToEdit'] == "Favorites" || values['thingToEdit'] == "Movies"; 

    } 
} 

지금 영화와 즐겨 찾기를 모두 편집 할 수있는 방법이 하나 있습니다. 매개 변수를 추가하기 만하면 편집중인 항목의 '유형'을 표시 할 수 있습니다.

당신이 당신의 현재 경로를 유지하기 원한다면, 당신은 다음을 수행 할 수 있어야한다 :

routes.MapRoute("UserEditsThings", 
    "user/{id}/edit{thingToEdit}", 
    new { controller = "UserController", action="Edit" }, 
    new { thingToEdit = ValidThingsToEditConstraint() } 
); 

내가 약 7 개월 동안 ASP.NET MVC에서 멀리 있었어요, 그래서이 조금 될 수있다 녹슨. 그것은 구문 오류에 대한 테스트를 거치지 않았으며 비단뱀이 빛을 발할 수도 있습니다. 그것은 당신을 거기에 가야한다.

+0

나는이 작업을 시도했지만 couldnt. 제가하려고하는 것을 구체적으로 설명하겠습니다. 나는 사용자 컨트 롤러가 있고 사용자 ID의 아이디를 가져 와서 액션이/index /에 설정되어 있습니다. 링크에서 제거하려면 routeconfig의 기본값으로 설정하십시오. 내가 갈 수있는 곳/User/Micheal/Edit/Favoites 어디서 만들었는지, 나는 문자열 사용자 이름을 취하는 index actionresult와 함께 UserController에서 Favorites ActionResult를 만들 수있다. –

+0

가장 좋은 방법은 가지고있는 경로와 원하는 컨트롤러 설정을 편집하는 것입니다.나는이 산문을 이해하지 못한다고 말해야 만하지만 코드를 게시하면 코드를 이해할 수 있습니다. –