2014-07-16 3 views
0

나는 Racing이라는 영역이 있습니다. 나는 아래와 같은 제약 조건을 사용하여 매개 변수를 허용하는 경로를 설정 한 :MVC 영역에 대한 라우팅 문제

글로벌 asax :

protected void Application_Start() 
    { 
     //AreaRegistration.RegisterAllAreas(); 

     WebApiConfig.Register(GlobalConfiguration.Configuration); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
     AuthConfig.RegisterAuth(); 
    } 

Route.config

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

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

     AreaRegistration.RegisterAllAreas(); 


    } 
} 

경주 지역 등록

public class RacingAreaRegistration : AreaRegistration 
    { 
     public override string AreaName 
     { 
      get 
      { 
       return "Racing"; 
      } 
     } 

     public override void RegisterArea(AreaRegistrationContext context) 
     { 
      // this maps to Racing/Meeting/Racecards/2014-01-06 and WORKS!! 
      context.MapRoute(
       name: "Racecard", 
       url: "Racing/{controller}/{action}/{date}", 
       defaults: new { controller="Meeting", action = "Racecards", date = UrlParameter.Optional }, 
       constraints: new { date = @"^\d{4}$|^\d{4}-((0?\d)|(1[012]))-(((0?|[12])\d)|3[01])$" } 
      ); 

      // this maps to Racing/Meeting/View/109 and WORKS!! 
      context.MapRoute(
       "Racing_default", 
       "Racing/{controller}/{action}/{id}", 
       defaults: new { controller="Meeting", action = "Hello", id = UrlParameter.Optional } 
      ); 





     } 
    } 

위 URL은 URL이 지정되어 있지만 이제는 Racing/Meeting/HelloWorld/1과 같은 매개 변수를 전달하지 않고도 Racing/Meeting/HelloWorld를 방문 할 수 없습니다. 어떤 아이디어?

고마워요.

+0

당신의 Global.asax에서 위해 Application_Start, 당신이 당신의 경로와 지역 노선 문제를 등록하는 순서를 표시 할 수있는 방법의 상부로 이동하려고합니다. – James

+0

경로 정의를 뒤집어 봤습니다. 즉 ID 경로를 만드십시오. 처음으로 정의 된 경로입니까? – Madullah

+0

예 나는 뒤집어 썼습니다. 전체 코드 편집을 참조하십시오. – CR41G14

답변

1

기본 경로보다 먼저 지역 등록을 완료해야합니다.

public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 

     AreaRegistration.RegisterAllAreas(); 


     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

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


    } 
} 
+0

고맙습니다! – CR41G14