MVC4区域注册路由



我对在一个区域注册路由感到困惑,基本上我有两个URL,我试图接受。一个默认,另一个接受日期作为字符串。

这是我的赛车区注册区号。

   public override void RegisterArea(AreaRegistrationContext context)
    {
        // URL Needed = Racing/Meeting/Racecards/2014-06-01
       // URL displayed = Racing/Meeting/Racecards/2014-06-01  // THIS WORKS!
        context.MapRoute(
            name: "Racecard",
            url: "Racing/{contoller}/{action}/{date}",
            defaults: new { controller="Meeting", action = "Racecards", date = UrlParameter.Optional }
        );

       // URL Needed = Racing/Meeting/View/109
       // URL displayed = Racing/Meeting/View?id=109
       context.MapRoute(
           "Racing_default",
           "Racing/{controller}/{action}/{id}",
            defaults: new { action = "Index", id = UrlParameter.Optional }
       );
    }

目前我不能让默认工作,现在我已经添加了第一条路由。如果我交换顺序,那么第一条路由不会传递参数回来。如有任何指导,将不胜感激。

编辑将路由更改为:

        context.MapRoute(
           name: "Racecard",
           url: "Racing/{contoller}/{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])$" }
       );

        context.MapRoute(
           "Racing_default",
           "Racing/{controller}/{action}/{id}",
            defaults: new { action = "Index", id = UrlParameter.Optional }

现在匹配这两个url并且它们工作。然而,当我访问一个标准的动作赛车/会议/或赛车/会议/HelloWorld,这两个现在都失败了。

你应该尝试在第一条路由中使用路由约束。

最新更新