C# ASP.NET路由-仅当为数字时添加路由?
我正在尝试使用ASP.NET路由生成路由,但我只希望在某些值为数字时应用它C# ASP.NET路由-仅当为数字时添加路由?,c#,asp.net,routing,C#,Asp.net,Routing,我正在尝试使用ASP.NET路由生成路由,但我只希望在某些值为数字时应用它 // Routing for Archive Pages routes.Add("Category1Archive", new Route("{CategoryOne}/{Year}/{Month}", new CategoryAndPostHandler())); routes.Add("Category2Archive", new Route("{CategoryOne
// Routing for Archive Pages
routes.Add("Category1Archive", new Route("{CategoryOne}/{Year}/{Month}", new CategoryAndPostHandler()));
routes.Add("Category2Archive", new Route("{CategoryOne}/{CategoryTwo}/{Year}/{Month}", new CategoryAndPostHandler()));
有没有办法知道{Year}和{Month}是否是数值。否则,此路由与其他路由冲突。您可以使用以下方法实现所需的筛选器:
Richard是对的,但我想补充一点:
好的,谢谢理查德和马丁为我指明了正确的方向。我最终需要的语法是:
routes.Add("Category1Archive", new Route("{CategoryOne}/{Year}/{Month}/", new CategoryAndPostHandler()) { Constraints = new RouteValueDictionary(new { Year = @"^\d+$", Month = @"^\d+$" }) });
routes.Add("Category2Archive", new Route("{CategoryOne}/{CategoryTwo}/{Year}/{Month}/", new CategoryAndPostHandler()) { Constraints = new RouteValueDictionary(new { Year = @"^\d+$", Month = @"^\d+$" }) });
routes.Add("Category1Archive", new Route("{CategoryOne}/{Year}/{Month}/", new CategoryAndPostHandler()) { Constraints = new RouteValueDictionary(new { Year = @"^\d+$", Month = @"^\d+$" }) });
routes.Add("Category2Archive", new Route("{CategoryOne}/{CategoryTwo}/{Year}/{Month}/", new CategoryAndPostHandler()) { Constraints = new RouteValueDictionary(new { Year = @"^\d+$", Month = @"^\d+$" }) });