2015-02-05 4 views
0

경로 또는 매개 변수에 매개 변수가없는 경우 기본 동작으로 리디렉션하는 것과 관련하여 몇 가지 문제가 있습니다.이 매개 변수는 제대로 작동하지만 다른 매개 변수를 추가하면 RedirectToAction이 작동하지 않고 throw됩니다. 오류 : 경로 테이블의 경로가 제공된 값과 일치하지 않습니다.Route의 매개 변수가 포함 된 RedirectToAction

[Route("First")] 
public ActionResult First() 
{ 
    //THIS DOESN'T WORK 
    //return RedirectToAction("Second", new { area = "plans"}); 

    //THIS WORKS 
    return RedirectToAction("Third", new { id = "12" }); 
} 

[Route("Second/{area}")] 
public ActionResult Second(string area) 
{ 
    return new ContentResult() { Content = "Second : " + area}; 
} 


[Route("Third/{id}")] 
public ActionResult Third(string id) 
{ 
    return new ContentResult() { Content = "Third " + id }; 
} 

그래서 내가/first를 입력하면 올바르게 리디렉션됩니다.

난 그냥 RouteConfig에 아무것도 추가하지 않습니다

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapMvcAttributeRoutes(); 
} 

답변

2

을 아마, 당신은 area 경로 매개 변수와 함께 area 매개 변수를 충돌하고 있습니다. 다음과 같은 정의와 Areas is a resource from Asp.Net MVC :

[Route("First")] 
public ActionResult First() 
{ 
    return RedirectToAction("Second", new { areaName= "plans"}); 
} 

[Route("Second/{areaName}")] 
public ActionResult Second(string areaName) 
{ 
    return new ContentResult() { Content = "Second : " + areaName}; 
} 
+0

당신은 맞다 :

Areas are logical grouping of Controller, Models and Views and other related folders for a module in MVC applications. By convention, a top Areas folder can contain multiple areas. Using areas, we can write more maintainable code for an application cleanly separated according to the modules.

이 매개 변수 이름을 변경하고 샘플, 사용을 시도 할 수는 areaareaName에 이름을 바꾸려고! 그것은 어떤 areaName을 사용하지만, 지역을 인수로 보내는 방법이 있습니까? Route ("{area}/Second")가 있고 다른 지역으로 리디렉션하는 방법을 가정 해 보겠습니다. – us3r

+0

동의. '{area}'는 사이트의 모든 영역을 라우팅 할 때 ASP.NET MVC에서 사용됩니다. 따라서 매개 변수의 이름을 혼란을 피하기 위해 다른 이름으로 바꾸는 것이 좋습니다. –

관련 문제