带有可空参数的路由显示为空路由值的查询字符串
本文关键字:路由 查询 字符串 显示 参数 | 更新日期: 2023-09-27 18:06:43
我有以下控制器动作:
public class Foo
{
public ActionResult Bar(int? barId)
{
...
}
}
这个动作对应的路由由:
routes.MapRoute("Foobar", "bar/{barId}",
new { controller = "Foo", action = "Bar", barId = UrlParameter.Optional },
new { barId = @"^[0-9]+$" });
在我的视图中,我生成的路由如下:
@Url.Action("Bar", "Foo", new { barId = bar.BarId })
对于bar.BarId = 32
,我收到/Foo/32
的期望URL
但我也想为null
值生成路由:
@Url.Action("Bar", "Foo", new { barId = (int?)null })
除此之外,我收到了/Foo?barId=currentBarId
的URL
其中currentBarId
是我当前正在查看的Bar
页面的barId
。
我做错了什么?
修改约束
new { barId = @"^[0-9]*$" }
这将允许此路由的barId
为空。