private ControllerContext BuildPageControllerContext(ControllerContext context) { string controllerName = _controllerMapper.GetControllerName(_thePage.GetContentType()); var routeData = context.RouteData; RouteExtensions.ApplyCurrentItem(routeData, controllerName, "Index", _thePage, null); if (context.RouteData.DataTokens.ContainsKey(ContentRoute.ContentPartKey)) { routeData.ApplyContentItem(ContentRoute.ContentPartKey, context.RouteData.DataTokens[ContentRoute.ContentPartKey] as ContentItem); } var requestContext = new RequestContext(context.HttpContext, routeData); var controller = (ControllerBase)ControllerBuilder.Current.GetControllerFactory() .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) { //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.AppRelativeCurrentExecutionFilePath); PathData td = engine.Resolve <RequestPathProvider>().ResolveUrl(url); var page = td.CurrentPage; var actionName = td.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)) { td.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)) { td.CurrentItem = part = engine.Persister.Get(partId); } } if (page == null && part == null) { return(null); } else if (page == null) { page = part.ClosestPage(); } var controllerName = 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.ApplyCurrentItem(data, controllerName, actionName, page, part); data.DataTokens[ContentEngineKey] = engine; return(data); }