private ControllerContext BuildPageControllerContext(ControllerContext context) { string controllerName = controllerMapper.GetControllerName(thePage.GetContentType()); RouteExtensions.ApplyCurrentPath(context.RouteData, controllerName, "Index", new PathData(thePage)); var requestContext = new RequestContext(context.HttpContext, context.RouteData); var controller = (ControllerBase)controllerFactory.CreateController(requestContext, controllerName); controller.ControllerContext = new ControllerContext(requestContext, controller); controller.ViewData.ModelState.Merge(context.Controller.ViewData.ModelState); return(controller.ControllerContext); }
private RouteData GetRouteDataForPath(HttpRequestBase request) { if (request == null) { throw new ArgumentNullException("request"); } if (request.Url == null) { throw new ArgumentException("Request has no URL", "request"); } //On a multi-lingual site with separate domains per language, //the full url (with host) should be passed to UrlParser.ResolvePath(): string host = (request.Url.IsDefaultPort) ? request.Url.Host : request.Url.Authority; var url = new Url(request.Url.Scheme, host, request.RawUrl); PathData path; var rpp = engine.Resolve <RequestPathProvider>(); if (rpp.IsRewritable(url) && rpp.IsObservable(url)) { path = rpp.ResolveUrl(url); } else { path = PathData.Empty; } if (!path.IsEmpty() && path.IsRewritable && StopRewritableItems) { return(new RouteData(this, new StopRoutingHandler())); } var page = path.CurrentPage; var actionName = path.Action; if (string.IsNullOrEmpty(actionName)) { actionName = request.QueryString["action"] ?? "Index"; } if (!string.IsNullOrEmpty(request.QueryString[PathData.PageQueryKey])) { int pageId; if (int.TryParse(request.QueryString[PathData.PageQueryKey], out pageId)) { path.CurrentPage = page = engine.Persister.Get(pageId); } } ContentItem part = null; if (!string.IsNullOrEmpty(request.QueryString[PathData.PartQueryKey])) { // part in query string is used to render a part int partId; if (int.TryParse(request.QueryString[PathData.PartQueryKey], out partId)) { path.CurrentItem = part = engine.Persister.Get(partId); path.Controller = null; } } else if (!string.IsNullOrEmpty(request.QueryString[PathData.ItemQueryKey])) { // this is a discrepancy between mvc and the legacy // in mvc the item query key doesn't route to the item, it's a marker // the urlparser however parses the item query key and passes the item as current in pathdata // hence this somewhat strange sidestepping int itemId; if (int.TryParse(request.QueryString[PathData.ItemQueryKey], out itemId)) { if (itemId == path.ID || (path.ID == 0 && path.CurrentItem != null && itemId == path.CurrentItem.VersionOf.ID)) { // we have an item id and it matches the path data we found via url parser // it hasn't been changed by a specific part query string so we reset it path.CurrentItem = path.CurrentPage; path.Controller = null; } } } if (page == null && part == null) { return(null); } path.CurrentPage = page ?? part.ClosestPage(); var controllerName = path.Controller ?? controllerMapper.GetControllerName((part ?? page).GetContentType()); if (controllerName == null) { return(null); } if (actionName == null || !controllerMapper.ControllerHasAction(controllerName, actionName)) { return(null); } var data = new RouteData(this, routeHandler); foreach (var defaultPair in innerRoute.Defaults) { data.Values[defaultPair.Key] = defaultPair.Value; } foreach (var tokenPair in innerRoute.DataTokens) { data.DataTokens[tokenPair.Key] = tokenPair.Value; } RouteExtensions.ApplyCurrentPath(data, controllerName, actionName, path); data.DataTokens[ContentEngineKey] = engine; return(data); }