作为 this question 的后续,我正在尝试实现自定义路由约束,以便为特定路由动态修改 RouteValueDictionary
。
我已经完成了 95%:我可以根据提供的参数模式匹配任何路由,匹配 url 中指定的操作。此过程的最后一步是覆盖 RouteValueDictionary 以将键重命名为 Controller 操作的适当参数。
这是我的代码的相关片段(整个类将近 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
方法时,如果指定的操作将返回 true在 url 中有两个两个整型参数,与实际参数名称无关。)
那么,有什么方法可以覆盖此请求的 RouteValueDictionary 吗?还是有其他方法可以做到这一点,但我错过了?
最佳答案
如果我理解正确,问题是什么:
So, is there a way I can overwrite the RouteValueDictionary for this request? Or is there some other way I can do this that I'm missing?
我采用了您的解决方案并更改了这些行以替换参数名称:
for (int i = 0; i < methodParameters.Length; i++)
{
// I. instead of this
//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
// II. do this (not so elegant but working;)
var key = unMappedList.ElementAt(i).Key;
var value = values[key];
values.Remove(key);
values.Add(methodParameters.ElementAt(i).Name, value);
// III. THIS is essential! if statement must be moved after the for loop
//return true;
}
return true; // here we can return true
注意:我喜欢您的方法。有时,扩展/调整路由然后转到代码并“修复不正确的参数名称”更为重要。当然,它们很可能是正确的。
这就是关注点分离的力量。
- URL 地址是一个地方。
- 另一个 Controller 、 Action 和参数名称。
- 以及具有强大自定义功能的路由...
...作为处理该问题的唯一正确位置。这就是关注点分离。不调整 Action 名称来服务 Url...
关于c# - 动态修改 RouteValueDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13423545/