/// <summary>Gets route data for for items this route handles.</summary> /// <param name="item">The item whose route to get.</param> /// <param name="routeValues">The route values to apply to the route data.</param> /// <returns>A route data object or null.</returns> public virtual RouteValueDictionary GetRouteValues(ContentItem item, RouteValueDictionary routeValues) { string actionName = "Index"; if (routeValues.ContainsKey(ActionKey)) { actionName = (string)routeValues[ActionKey]; } string controllerName = controllerMapper.GetControllerName(item.GetContentType()); if (controllerName == null || !controllerMapper.ControllerHasAction(controllerName, actionName)) { return(null); } var values = new RouteValueDictionary(routeValues); foreach (var kvp in innerRoute.Defaults) { if (!values.ContainsKey(kvp.Key)) { values[kvp.Key] = kvp.Value; } } values[ControllerKey] = controllerName; values[ActionKey] = actionName; values[ContentItemKey] = item.ID; values[AreaKey] = innerRoute.DataTokens["area"]; return(values); }
private RouteValueDictionary GetRouteValues(HtmlHelper helper, ContentItem item) { Type itemType = item.GetContentType(); string controllerName = controllerMapper.GetControllerName(itemType); if (string.IsNullOrEmpty(controllerName)) { Engine.Logger.WarnFormat("Found no controller for type {0}", itemType); return(null); } var values = new RouteValueDictionary(); values[ContentRoute.ActionKey] = "Index"; values[ContentRoute.ControllerKey] = controllerName; if (item.ID != 0) { values[ContentRoute.ContentItemKey] = item.ID; } else { values[ContentRoute.ContentItemKey] = item; } // retrieve the virtual path so we can figure out if this item is routed through an area var vpd = helper.RouteCollection.GetVirtualPath(helper.ViewContext.RequestContext, values); if (vpd == null) { throw new InvalidOperationException("Unable to render " + item + " (" + controllerName + " did not match any route)"); } values["area"] = vpd.DataTokens["area"]; return(values); }
public PathData GetPath(ContentItem item, string remainingUrl) { int slashIndex = remainingUrl.IndexOf('/'); string action = remainingUrl; string arguments = null; if (slashIndex > 0) { action = remainingUrl.Substring(0, slashIndex); arguments = remainingUrl.Substring(slashIndex + 1); } var controllerName = controllerMapper.GetControllerName(item.GetContentType()); if (string.IsNullOrEmpty(action) || string.Equals(action, "Default.aspx", StringComparison.InvariantCultureIgnoreCase)) { action = "Index"; } foreach (string method in methods) { if (string.Equals(method, action, StringComparison.InvariantCultureIgnoreCase)) { return(new PathData(item, null, action, arguments) { IsRewritable = false, TemplateUrl = string.Format("~/{0}/{1}", controllerName, method, item.ID) // workaround for start pages }); } } return(null); }
public IPathData ResolvePath(string virtualUrl) { // Set the default action to index _pathData.Action = ContentRoute.DefaultAction; // Set the default controller to content _pathData.Controller = ContentRoute.DefaultControllerName; // Get an up to date document session from structuremap _session = ObjectFactory.GetInstance <IDocumentSession>(); // The requested url is for the start page with no action if (string.IsNullOrEmpty(virtualUrl) || string.Equals(virtualUrl, "/")) { _pageModel = _session.Query <IPageModel>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .SingleOrDefault(x => x.Parent == null); _pathData.CurrentPageModel = _pageModel; return(_pathData); } // Remove the trailing slash virtualUrl = VirtualPathUtility.RemoveTrailingSlash(virtualUrl); // The normal beahaviour should be to load the page based on the url _pageModel = _session.Query <IPageModel, Document_ByUrl>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .FirstOrDefault(x => x.Metadata.Url == virtualUrl); // Try to load the page without the last segment of the url and set the last segment as action)) if (_pageModel == null && virtualUrl.LastIndexOf("/", System.StringComparison.Ordinal) > 0) { var index = virtualUrl.LastIndexOf("/", System.StringComparison.Ordinal); var action = virtualUrl.Substring(index, virtualUrl.Length - index).Trim(new[] { '/' }); virtualUrl = virtualUrl.Substring(0, index).TrimStart(new[] { '/' }); _pageModel = _session.Query <IPageModel, Document_ByUrl>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .FirstOrDefault(x => x.Metadata.Url == virtualUrl); _pathData.Action = action; } // If the page model still is empty, let's try to resolve if the start page has an action named (virtualUrl) if (_pageModel == null) { _pageModel = _session.Query <IPageModel>().SingleOrDefault(x => x.Parent == null); var controllerName = _controllerMapper.GetControllerName(typeof(ContentController)); var action = virtualUrl.TrimStart(new[] { '/' }); if (!_controllerMapper.ControllerHasAction(controllerName, action)) { return(null); } _pathData.Action = action; } if (_pageModel == null) { return(null); } _pathData.CurrentPageModel = _pageModel; return(_pathData); }
public ControllerWrapper(ContentItem item, IControllerMapper controllerMapper) { this.ID = "cw" + item.ID; ViewData = new ViewDataDictionary(item); this.item = item; this.controllerMapper = controllerMapper; itemType = item.GetContentType(); controllerName = controllerMapper.GetControllerName(itemType); routes = RouteTable.Routes; httpContext = new HttpContextWrapper(HttpContext.Current); }
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); }
public void RenderTemplate(ContentItem item, HtmlHelper helper) { Type itemType = item.GetContentType(); string controllerName = controllerMapper.GetControllerName(itemType); if (string.IsNullOrEmpty(controllerName)) { Trace.TraceWarning("Found no controller for type " + itemType); return; } RouteValueDictionary values = GetRouteValues(helper, item, controllerName); helper.RenderAction("Index", values); }
public PathData GetPath(ContentItem item, string remainingUrl) { int slashIndex = remainingUrl.IndexOf('/'); string action = remainingUrl; string arguments = null; if (slashIndex > 0) { action = remainingUrl.Substring(0, slashIndex); arguments = remainingUrl.Substring(slashIndex + 1); } var controllerName = this.controllerName ?? controllerMapper.GetControllerName(item.GetContentType()); if (string.IsNullOrEmpty(action) || string.Equals(action, "Default.aspx", StringComparison.InvariantCultureIgnoreCase)) { action = "Index"; } foreach (string method in methods) { if (string.Equals(method, action, StringComparison.InvariantCultureIgnoreCase)) { string templateUrl = item.VersionOf.HasValue ? string.Format("~/{0}/{1}", controllerName, method, item.VersionOf.ID) : string.Format("~/{0}/{1}", controllerName, method, item.ID); // workaround for start pages var pd = new PathData(item) { IsRewritable = false, Controller = controllerName, Action = action, Argument = arguments, TemplateUrl = templateUrl }; return(pd); } } return(null); }
private ControllerContext BuildPageControllerContext(ControllerContext context) { string controllerName = _controllerMapper.GetControllerName(_thePage.GetContentType()); var routeData = context.RouteData; routeData.ApplyCurrentItem(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); }
/// <summary> /// Resolves the path. /// </summary> /// <param name="routeData">The route data.</param> /// <param name="virtualUrl">The virtual URL.</param> /// <returns></returns> public IPathData ResolvePath(RouteData routeData, string virtualUrl) { // Set the default action to index _pathData.Action = UIRoute.DefaultAction; // Get an up to date document session from structuremap _session = _container.GetInstance <IDocumentSession>(); // The requested url is for the start page with no action if (string.IsNullOrEmpty(virtualUrl) || string.Equals(virtualUrl, "/")) { _pageModel = _session.Query <IPageModel>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .SingleOrDefault(x => x.Parent == null); } else { // Remove the trailing slash virtualUrl = VirtualPathUtility.RemoveTrailingSlash(virtualUrl).TrimStart(new[] { '/' }); // The normal beahaviour should be to load the page based on the url _pageModel = _session.Query <IPageModel, PageByUrl>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .FirstOrDefault(x => x.Metadata.Url == virtualUrl); // Try to load the page without the last segment of the url and set the last segment as action)) if (_pageModel == null && virtualUrl.LastIndexOf("/") > 0) { var index = virtualUrl.LastIndexOf("/"); var action = virtualUrl.Substring(index, virtualUrl.Length - index).Trim(new[] { '/' }); virtualUrl = virtualUrl.Substring(0, index).TrimStart(new[] { '/' }); _pageModel = _session.Query <IPageModel, PageByUrl>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .FirstOrDefault(x => x.Metadata.Url == virtualUrl); _pathData.Action = action; } // If the page model still is empty, let's try to resolve if the start page has an action named (virtualUrl) if (_pageModel == null) { _pageModel = _session.Query <IPageModel>().SingleOrDefault(x => x.Parent == null); if (_pageModel == null) { return(null); } var pageTypeAttribute = _pageModel.GetType().GetAttribute <PageTypeAttribute>(); object area; _controllerName = _controllerMapper.GetControllerName(routeData.Values.TryGetValue("area", out area) ? typeof(PagesController) : pageTypeAttribute.ControllerType); var action = virtualUrl.TrimStart(new[] { '/' }); if (!_controllerMapper.ControllerHasAction(_controllerName, action)) { return(null); } _pathData.Action = action; } } if (_pageModel == null) { return(null); } var controllerType = _pageModel.GetType().GetAttribute <PageTypeAttribute>().ControllerType; _pathData.Controller = controllerType != null?_controllerMapper.GetControllerName(controllerType) : string.Format("{0}Controller", _pageModel.GetType().Name); _pathData.CurrentPage = _pageModel; _pathData.NavigationContext = _session.GetPublishedPages(_pageModel.Id); return(_pathData); }