区域注册中的MVC4路由

时间:2014-07-16 09:37:05

标签: c# asp.net asp.net-mvc asp.net-mvc-4

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

以下是我所在区域的注册区域代码,名为Racing。

   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 }

现在匹配两个网址并且它们可以正常工作。然而,当我访问标准动作赛车/会议/或赛车/会议/ HelloWorld时,这些现在都失败了。

1 个答案:

答案 0 :(得分:1)

您应该尝试对第一条路线使用路线约束。请参阅此atricle以获取帮助。