Optional routing parameter with constraint in ASP.NET MVC 2?

后端 未结 2 453
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 17:30

If I have a route like this:

routes.Add(new Route(\"{controller}/{page}\", 
    new RouteValueDictionary
    {
        { \"page\", UrlParameter.Optional }
           


        
2条回答
  •  情话喂你
    2021-01-17 17:54

    I'm using ^$| within a regex, such as: (^$|[Pp]age\d+). I found this question while searching for an answer to this and figured I'd add what I found here.

    routes.MapRoute(
      name: "News Archive",
      url: "News/{page}",
      defaults: new { controller = "news", action = "List", page= UrlParameter.Optional },
      constraints: new { page= @"^$|[0-9][0-9]" });
    

提交回复
热议问题