2014-07-16 153 views
0

我有一个区域叫做赛车。我已经设置了路线,接受使用如下的约束参数: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,但现在我无法像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