private static RouteData CreateRouteData(RouteBase route, RouteValueDictionary routeValues, RouteValueDictionary dataTokens, ViewContext parentViewContext) { RouteData routeData = new RouteData(); foreach (KeyValuePair <string, object> kvp in routeValues) { routeData.Values.Add(kvp.Key, kvp.Value); } foreach (KeyValuePair <string, object> kvp in dataTokens) { routeData.DataTokens.Add(kvp.Key, kvp.Value); } routeData.Route = route; routeData.DataTokens[ControllerContext.ParentActionViewContextToken] = parentViewContext; // It's possible that the outgoing route is a direct route - in which case it's not possible to reach using // the action name and controller name. We need to check for that case to determine if we need to create a // 'direct route' routedata to reach it. if (route.IsDirectRoute()) { return(RouteCollectionRoute.CreateDirectRouteMatch(route, new List <RouteData>() { routeData })); } else { return(routeData); } }
private static RouteData CreateRouteData( RouteBase route, RouteValueDictionary routeValues, RouteValueDictionary dataTokens, ViewContext parentViewContext ) { RouteData routeData = new RouteData(); foreach (KeyValuePair <string, object> kvp in routeValues) { routeData.Values.Add(kvp.Key, kvp.Value); } foreach (KeyValuePair <string, object> kvp in dataTokens) { routeData.DataTokens.Add(kvp.Key, kvp.Value); } routeData.Route = route; routeData.DataTokens[ControllerContext.ParentActionViewContextToken] = parentViewContext; // It's possible that the outgoing route is a direct route - in which case it's not possible to reach using // the action name and controller name. We need to check for that case to determine if we need to create a // 'direct route' routedata to reach it. if (route.IsDirectRoute()) { // Codeplex-2136 - ControllerContext.IsChildAction returns false inside Controller.Initialize() // // We're constructing a 'temp' route data to wrap the route data for the match we're invoking via // an attribute route. The ControllerContext will look at datatokens to see if it's being invoked // as a child action. // // By sticking the view context on both route data ControllerContext will do the right thing // at all parts of the pipeline. var directRouteData = RouteCollectionRoute.CreateDirectRouteMatch( route, new List <RouteData>() { routeData } ); directRouteData.DataTokens[ControllerContext.ParentActionViewContextToken] = parentViewContext; return(directRouteData); } else { return(routeData); } }