[英]ASP.NET MVC Custom optional parameter
How is it possible to define a route in ASP.NET MVC 5 that will ignore static prefixes/suffixes (like "zip-"
, "-county"
) for optional parameters if the latter not set? 如果未设置可选参数,如何在ASP.NET MVC 5中定义一个路由,该路由将忽略静态前缀/后缀(例如"zip-"
, "-county"
)?
So that in this case for example: 因此,在这种情况下,例如:
routes.Add("Search",
new LowercaseDashedRoute(
"search/{searchType}/{state}/{county}-county/{city}",
new RouteValueDictionary(new {
controller = "Home",
action = "Search",
county = UrlParameter.Optional,
city = UrlParameter.Optional }),
new RouteValueDictionary(new { searchType = searchTypeConstraint }),
new LowercaseDashedRouteHandler()));
when county
is missing the URL would be like /search/sale/ca/los-angeles
but not /search/sale/ca/-county/los-angeles
如果缺少county
则URL类似于/search/sale/ca/los-angeles
而不是 /search/sale/ca/-county/los-angeles
You can set a constraint for your county parameter and then create another route without it: 您可以为县县参数设置约束,然后在没有约束的情况下创建另一条路线:
routes.Add("Search",
new LowercaseDashedRoute(
"search/{searchType}/{state}/{county}/{city}",
new RouteValueDictionary(new {
controller = "Home",
action = "Search",
county = UrlParameter.Optional,
city = UrlParameter.Optional }),
new RouteValueDictionary(new { searchType = searchTypeConstraint, county="\\S+-county" }),
new LowercaseDashedRouteHandler()));
Route 2: 路线2:
routes.Add("Search",
new LowercaseDashedRoute(
"search/{searchType}/{state}/{city}",
new RouteValueDictionary(new {
controller = "Home",
action = "Search",
city = UrlParameter.Optional }),
new RouteValueDictionary(new { searchType = searchTypeConstraint }),
new LowercaseDashedRouteHandler()));
The downside is that "-county" suffix will be added to the parameter, but you can easily handle this in your controller. 缺点是参数将添加“ -county”后缀,但是您可以在控制器中轻松处理此后缀。
It is not possible to have 2 optional parameters on a single route or to combine an optional parameter with a literal part in a segment. 在单个路径上不可能有2个可选参数,也不能将可选参数与段中的文字部分组合。
However, to get the desired effect, you can just add another route to handle the case with no county (and remove the county as an optional parameter). 但是,要获得理想的效果,您可以添加另一条路线来处理没有县的情况(并删除县作为可选参数)。
// Matches:
// /search/sale/ca/los-angeles-county/los-angeles
// /search/sale/ca/los-angeles-county
routes.Add("SearchCounty",
new LowercaseDashedRoute(
"search/{searchType}/{state}/{county}-county/{city}",
new RouteValueDictionary(new {
controller = "Home",
action = "Search",
city = UrlParameter.Optional }),
new RouteValueDictionary(new { searchType = searchTypeConstraint }),
new LowercaseDashedRouteHandler()));
// Matches:
// /search/sale/ca/los-angeles
routes.Add("Search",
new LowercaseDashedRoute(
"search/{searchType}/{state}/{city}",
new RouteValueDictionary(new {
controller = "Home",
action = "Search" }),
new RouteValueDictionary(new { searchType = searchTypeConstraint }),
new LowercaseDashedRouteHandler()));
The routing engine will only match the first route if <something>-county
is passed in as the 4th segment, so it will be able to distinguish that case from just passing a city
argument. 如果将<something>-county
作为第4段传入,则路由引擎将仅与第一个路由匹配,因此它将能够通过仅传递city
参数来区分这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.