ASP.NET MVC 路由与可选 URL 参数的映射问题
routes.MapRoute("Archive.CityState",
"Archive/{City}/{State}/{OP1}/{OP2}",
new { controller = "Archive", action = "CityState", OP1 = UrlParameter.Optional, OP2 = UrlParameter.Optional },
new { City="[a-zA-Z]+" ,State = @"[a-zA-Z]{2}", OP1 = @"[a-zA-Z]+" , OP2 =@"\d{4}" });
public ActionResult CityState(string City, string State, string OP1, int OP2)
{
var x = City;
var y = State;
var OptionalParameter1= OP1;
var OptionalParameter2 = OP2;
return View();
}
这个可以映射
Archive/Remond/WA/Chemistry
Archive/Remond/WA/Chemistry/2010
但不是
Archive/Remond/WA
有人可以帮忙吗?谢谢。
routes.MapRoute("Archive.CityState",
"Archive/{City}/{State}/{OP1}/{OP2}",
new { controller = "Archive", action = "CityState", OP1 = UrlParameter.Optional, OP2 = UrlParameter.Optional },
new { City="[a-zA-Z]+" ,State = @"[a-zA-Z]{2}", OP1 = @"[a-zA-Z]+" , OP2 =@"\d{4}" });
public ActionResult CityState(string City, string State, string OP1, int OP2)
{
var x = City;
var y = State;
var OptionalParameter1= OP1;
var OptionalParameter2 = OP2;
return View();
}
This can map
Archive/Remond/WA/Chemistry
Archive/Remond/WA/Chemistry/2010
But not
Archive/Remond/WA
Can anyone help? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些限制使其无法匹配您想要的内容:
因为这两个参数都是必需的。如果您希望此路由能够与 Archive/Remond/WA 匹配,请修改正则表达式以接受空值。另请记住,只有 last 参数可以是可选的(不能有两个连续的可选参数,因为这没有意义)。
These constraints prevent it from matching what you want:
As both parameters are required. Modify your regular expression to accept empty values if you want this route to be able to be matched by
Archive/Remond/WA
. Also remember that only the last parameter can be optional (you cannot have two successive optional parameters as this makes no sense).