需要帮助使用正则表达式来约束路线

时间:2012-03-12 13:00:43

标签: c# asp.net regex asp.net-mvc-3

我怎样才能最好地说明L1Cat和L2Cat的值不能是整数值/不仅仅包含0-9个数字,以防止页面值被一些URL请求分配给它们。

                    routes.MapRoute(
        "Store5", // Route name
        "{RootPointer}/{L1Cat}/{L2Cat}/{page}", // URL with parameters
        new
        {
            controller = "Store",
            action = "Index",
            RootPointer = UrlParameter.Optional,
            page =1
        },
        new { controller = "Store", action = "Index", page = @"\d+" });

2 个答案:

答案 0 :(得分:0)

对L1Cat和L2Cat都尝试@".*[^\d].*"。这意味着在url部分的某处必须有非十进制字符。通常情况下[^\d]就足够了,但默认路由包含^$中的正则表达式约束,因此只有[^\d]才能捕获单个非数字部分。< / p>

答案 1 :(得分:0)

试试这个

 !Regex.IsMatch(url,@"/\d+/")
相关问题