我在 global.asax 中定义了两条路线,如下所示
context.MapRoute("HomeRedirect", "",
new
{
controller = "Home",
action = "redirect"
});
context.MapRoute("UrlResolver", "{culture}/some",
new
{
culture = "en-gb",
controller = "someController",
action = "someAction"
},
new
{
culture = new CultureRouteConstraint()
});
根据上面的定义,当用户请求 mysite.com/ 时 HomeController 的重定向动作应该被调用,其中:
public class HomeController : Controller
{
public ActionResult Redirect()
{
return RedirectToRoute("UrlResolver");
}
}
我想将用户重定向到上面第二个定义的路由,所以我也为它指定了默认值,并为每一个指定了一些约束。但是当RedirectToRoute("UrlResolver")
转弯时,没有默认值传递给第二条路线上的 routeConstraints 并No route in the route table matches the supplied values
显示。
更新
我的文化路线约束:
public class CultureRouteConstraint : IRouteConstraint
{
bool IRouteConstraint.Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
try
{
var parameter = values[parameterName] as string;
return (someCondition(parameter));
}
catch
{
return false;
}
}
}
现在值参数没有文化键/值,但路由参数有。