在ASP.NET MVC 2约束可选路由参数? [英] Optional routing parameter with constraint in ASP.NET MVC 2?

查看:102
本文介绍了在ASP.NET MVC 2约束可选路由参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个这样的路线:

If I have a route like this:

routes.Add(new Route("{controller}/{page}", 
    new RouteValueDictionary
    {
        { "page", UrlParameter.Optional }
    },
    new RouteValueDictionary
    {
        { "page", @"[Pp]age\d+" }
    }, 
    new MvcRouteHandler()
));

然后路由时不{PAGE}缺少匹配,但是如果我删除它匹配的约束。这是一个错误或功能?

Then the route doesn't match when {page} is missing, however if I remove the constraint it matches. Is this a bug or a feature?

推荐答案

这是一个特点:怎么能约束匹配,如果如果该参数可选?你可能不是要设置页的默认值第1页,以解决您的问题,或更换您的正则表达式([PP]年龄\\ D +)?允许没有匹配(我不知道这一个,不能ATM测试)。

It's a feature: how can the constraint match if the parameter if optional? You might either want to set the default value for "page" to "Page1" to resolve your problem, or replace your regex with "([Pp]age\d+)?" to allow nothing to match (I'm not sure about this one and can't test it atm).

这篇关于在ASP.NET MVC 2约束可选路由参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆