2014-11-17 2 views
1

RedirectToRoute()를 사용하여 리디렉션하려고하면 특정 사례의 기본 경로가 작동하지 않습니다. RouteConfig.cs에 따라, 요리가있는 URL을 식별 할 때마다 우선 순위가 Cuisine에 있습니다. 요리 컨트롤러에 액세스하는 동안 특정 시나리오에서 기본 경로를 사용하고 싶습니다. 요리 컨트롤러에서 동작을 리디렉션 으로 리디렉션해야합니다. 그것은 가능합니까?MVC에서 RedirectToRoute가 기본 경로를 사용하여 리디렉션되지 않습니다.

코드 조각 : http://app.com/cuisine/redirect 경기 모두 :

RouteConfig.cs

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

     routes.MapRoute(
      "Cuisine", 
      "cuisine/{name}", 
      new { controller = "Cuisine", action = "Search", name = UrlParameter.Optional }); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 

CuisineController.cs

public class CuisineController : Controller 
{ 
    // GET: Cuisine 
    public ActionResult Search(string name) 
    { 
     return RedirectToRoute("Default", new { controller = "Cuisine", action = "Redirect" }); 
    } 

    public ActionResult Redirect() 
    { 
     return Content("Restaurant Closed"); 
    } 
} 

답변

0

리디렉션은 당신이 가지고있는 문제 즉 제대로 생성 규칙 및 :

routes.MapRoute(
      "Cuisine", 
      "cuisine/{name}", 
      new { controller = "Cuisine", action = "Search", name = UrlParameter.Optional }); 

이 루트 테이블에서 첫 번째로 사용됩니다. 당신은 단순히 Cuisine 일 전에 리디렉션에 대한 추가 경로 설정을 추가하여 문제를 해결할 수 있습니다

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapRoute(
       "Cuisine-redirect", 
       "cuisine/redirect", 
       new { controller = "Cuisine", action = "Redirect"}); 
    routes.MapRoute(
       "Cuisine", 
       "cuisine/{name}", 
       new { controller = "Cuisine", action = "Search", name = UrlParameter.Optional }); 

    routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 

Cuisine-redirect 경로가 첫 번째 일치 및 실행됩니다 이런 식으로.

+0

빠른 응답을 보내 주셔서 감사합니다. 추가 한 MapRoute는 완전히 하드 코딩 된 것입니다. RedirectX라는 추가 작업이 있으면 작동하지 않습니다. 기본 경로를 강제 할 수있는 방법이 있습니까? –

+0

예.하지만 리디렉션 URL을 변경해야합니다. http://appurl.com/cuisine/redirect는 'Cuisine' 경로와 일치하므로 사용할 수 없습니다. http://appurl.com/cuisine/name/redirect 또는 추가 경로가있는 항목을 사용할 수 있습니다. 추가 매개 변수를 조정하려면 특성 라우팅을 사용하십시오. 또는 단순히 '요리 경로'를 http://appurl.com/cuisine/search/name을 가리 키도록 변경하십시오. 이렇게하면 문제를 완전히 없애 버릴 수 있습니다 (물론 가능하다면) – b2zw2a

0

코드 대신 아래 코드를 시도하십시오 RedirectRoute에서

할 수 있습니다 다음과 같은 제 컨트롤러 이름에 액션 이름을 지정하여 아래의 코드도

를 사용하여 리디렉션 할 수. 대담한 부분을 자세히 살펴보십시오.

관련 문제