路径中第一个参数的默认值

时间:2011-03-09 12:11:11

标签: asp.net-mvc routing

我有多语言网站的流派,标签,风格等画作。

看起来不错,比如

en/Genre/allegorical-painting

但是,客户想要的是将 en 排除在英语之外,只需

Genre/allegorical-painting

我将“en”作为默认值,但它会一直呈现...当我手动删除它时,我有404错误。这是我的规则定义:

var searchActionsConstraint = new RouteValueDictionary { { "action", "Style|Tag|Painting-School|Art-Movement|Artist-by-Genre|Gallery|Search|Genre|Technique|Material" }, {"lang", "en|es|fr"} };
DomainRoute wikiSearchRoute = new Route("{lang}/{action}/{SearchTerm}/{CurrentPage}/{SearchCategory}/{searchTextMode}",
new RouteValueDictionary(new
{
 lang = "en",
 controller = "WikiSearch",
 action = "",
 SearchTerm = "Any",
 CurrentPage = "1",
 SearchCategory = "Painting",
 searchTextMode = "SearchTags"
}), searchActionsConstraint, new DomainRouteHandler());

RouteTable.Routes.Add("WikiSearchRoute", wikiSearchRoute);

一般情况下,如何在MVC中隐藏默认路由参数?

1 个答案:

答案 0 :(得分:4)

您可以添加不带lang的第二条路线,并为其指定默认路径

DomainRoute wikiSearchRouteEn = new Route("{action}/{SearchTerm}/{CurrentPage}/{SearchCategory}/{searchTextMode}",
new RouteValueDictionary(new
{
 lang = "en",
 controller = "WikiSearch",
 action = "",
 SearchTerm = "Any",
 CurrentPage = "1",
 SearchCategory = "Painting",
 searchTextMode = "SearchTags"
}), searchActionsConstraint, new DomainRouteHandler());

RouteTable.Routes.Add("WikiSearchRouteEn", wikiSearchRouteEn);