2011-08-09 4 views
1

내가이 경로를 매개 변수 링크 :ASP.NET MVC 문제

@Html.ActionLink("Friends", "Index", "Friends") 
@Html.ActionLink("Information", "Index", "UserInfo", new { userId = (string)null },null) 
,691,363,210

MVC 재사용 쿼리 매개 변수 첫번째 생성 된 링크 : 그래서

my_site/사용자/197/친구

제 2 링크 생성된다

my_site/사용자 정보? userId = 197

왜 두 번째 링크의 userId 값은 197입니까? 왜 링크가 없습니다 :

my_site를/사용자 정보

답변

1

내가 MVC가 어떤 경로 여기가 손에이 값하지만, 재사용하기로 결정 정확한 위치를 내 프로젝트에 사용할 무엇을 찾아 낼 수 없습니다

// 
// This fixes "ambient values" problem: 
// http://stackoverflow.com/questions/2651675/asp-net-mvc-html-actionlink-maintains-route-values 
// http://stackoverflow.com/questions/780643/asp-net-mvc-html-actionlink-keeping-route-value-i-dont-want 
return new UrlHelper(
    new RequestContext(
     HttpContext.Current, 
     new RouteData { 
      Route = urlHelper.RequestContext.RouteData.Route, 
      RouteHandler = urlHelper.RequestContext.RouteData.RouteHandler 
     }), urlHelper.RouteCollection) 
    .Action(actionName, controllerName, routeValuesWithArea); 

여기서 핵심은 그 어느 RouteData.DataTokensRouteData.Values이 설정되지 않았으므로 MVC가 재사용 할 수있는 것이 없습니다.

+0

링크를 생성하는 데 사용되는 방법을 볼 수 있습니까? – DeeRain

0

아마 할 것 내가 더 내가 ASP.NET 라우팅 규칙을 흔들려고 찾을

http://foo.com/user/events/197 

http://foo.com/user/events?userId=197 

같은 내 앱을 개발할 시간을 더 보낼 수 있습니다.

public class UserController : Controller 
{ 
    public ActionResult Events(long userId) 
    { 
     //Do Something... 
    } 
} 

public class FriendsController : Controller 
{ 
    public ActionResult Index(long userId) 
    { 
     //Do Something... 
    } 
} 
+0

질문을 이해할 수 없다고 생각합니다. – DeeRain