解决方法
HttpContext将保存对MvcHandler的引用,该引用将保存对RouteData的引用,该引用将保存对特定路由正在调用什么控制器的引用。
NB: This doesn’t give you the actual controller,only the controller
that the specific route is going to
catch.
GetController(HttpContextBase httpContext) { var routeData = ((MvcHandler)httpContext.Handler).RequestContext.RouteData; var routeValues = routeData.Values; var matchedRouteBase = routeData.Route; var matchedRoute = matchedRouteBase as Route; if (matchedRoute != null) { Route = matchedRoute.Url ?? string.Empty; } AssignRouteValues(httpContext,routeValues); } protected virtual VirtualPathData getVirtualPathData(HttpContextBase httpContext,RouteValueDictionary routeValues) { return RouteTable.Routes.GetVirtualPath(((MvcHandler)httpContext.Handler).RequestContext,routeValues); } private void AssignRouteValues(HttpContextBase httpContext,RouteValueDictionary routeValues) { var virtualPathData = getVirtualPathData(httpContext,routeValues); if (virtualPathData != null) { var vpdRoute = virtualPathData.Route as Route; if (vpdRoute != null) { RouteDefaults = vpdRoute.Defaults; RouteConstraints = vpdRoute.Constraints; RouteDataTokens = virtualPathData.DataTokens; RouteValues = routeValues; } } }
这段代码可能看起来很熟悉,这是因为我已经适应了Phil Haack的route debugger源代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。