我创建了一个名为的自定义路由CustomRoute
,该路由对方法执行各种操作GetVirtualPath
(对于本示例,我将整个 URL 设置为小写),其结构如下:
public sealed class CustomRoute : Route
{
public CustomRoute(string url, IRouteHandler routeHandler) : base(url, routeHandler) { }
public CustomRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler) { }
public CustomRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler) : base(url, defaults, constraints, routeHandler) { }
public CustomRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) : base(url, defaults, constraints, dataTokens, routeHandler) { }
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData path = base.GetVirtualPath(requestContext, values);
if (path != null)
{
path.VirtualPath = path.VirtualPath.ToLowerInvariant();
}
return path;
}
}
我通过一个名为的扩展方法将此路由添加到我的路由集合中MapCustomRoute
:
public static CustomRoute MapCustomRoute(this RouteCollection routes, string url, object defaults)
{
CustomRoute route = new CustomRoute(url, new RouteValueDictionary(defaults), new MvcRouteHandler());
routes.Add(route);
return route;
}
RouteTable.Routes
然后最后在 global.asax 中的集合上调用这个扩展:
RouteTable.Routes.Clear();
RouteTable.Routes.MapCustomRoute("somecontroller/someaction/{id}", new { controller = "Home", action = "Index" });
路由工作正常,我的请求正确映射到相应的控制器和操作 - 但是 URL 仍然是大写的。单步执行我的代码,我可以看到CustomRoute
在初始化路由时调用了我的构造函数 - 但GetVirtualPath
在发出请求时从未调用该方法。我是否在这里遗漏了一些明显的东西,我尝试使用路由调试工具,一切看起来都很好,所以我在这里不知所措!