Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET路由-仅当为数字时添加路由?_C#_Asp.net_Routing - Fatal编程技术网

C# 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

我正在尝试使用ASP.NET路由生成路由,但我只希望在某些值为数字时应用它

        // 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+$" }) });