作为这个问题的后续,我正在尝试实现自定义路由约束,以便RouteValueDictionary
为特定路由动态修改。
我已经完成了 95%:我可以根据提供的参数模式匹配任何路由,匹配 url 中指定的操作。此过程的最后一步是覆盖 RouteValueDictionary 以将键重命名为控制器操作的适当参数。
这是我的代码的相关片段(整个类将近 300 行,所以我不想全部添加,但如果需要我可以添加):
public class CustomRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string paramName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection.Equals(RouteDirection.UrlGeneration)) {
return false;
}
//this will grab all of the "actual" parameters out of the RVD, excluding action and controller
Dictionary<string, object> unMappedList = values.Where(x => x.Key.Contains("param")).OrderBy(xi => xi.Key).ToDictionary(
kvp => kvp.Key, kvp => kvp.Value);
string controller = values["controller"] as string;
string action = values["action"] as string;
//this method tries to find the controller using reflection
Type cont = TryFindController(controller);
if (cont != null) {
MethodInfo actionMethod = cont.GetMethod(action);
if (actionMethod != null) {
ParameterInfo[] methodParameters = actionMethod.GetParameters();
//this method validates that the parameters of the action that was found match the expected parameters passed in the custom constraint; it also performs type conversion
if (validateParameters(methodParameters, unMappedList)) {
for (int i = 0; i < methodParameters.Length; i++) {
values.First(x => x.Key == unMappedList.ElementAt(i).Key).Key = methodParameters.ElementAt(i).Name;
//above doesn't work, but even if it did it wouldn't do the right thing
return true;
}
}
}
}
return false;
}
}
以下是我使用自定义约束的方式:
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/{param1}-{param2}",
defaults: new { controller = "Admin", action = "Index" },
constraints: new { lang = new CustomRouteConstraint(new RoutePatternCollection( new List<ParamType> { ParamType.INT, ParamType.INT })) }
);
(我传递给构造函数的基本上是在说“要查找的参数模式是两个整数”。所以当Match
调用该方法时,如果 url 中指定的操作有两个整数参数,它将返回 true,而不管实际参数名称。)
那么,有没有办法可以覆盖这个请求的 RouteValueDictionary ?还是有其他我想念的方法可以做到这一点?