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 async Task <IResolveResult> Resolve(RouteContext context, RequestCulture requestCulture) { var trie = await _routeResolverTrie.LoadTrieAsync(requestCulture); if (trie == null || !trie.Any()) { return(null); } // Set the default action to index var action = DefaultRouter.DefaultAction; PathString remaining = context.HttpContext.Request.Path; if (context.HttpContext.Request.Path.Value.StartsWith("/" + requestCulture.Culture.TwoLetterISOLanguageName, StringComparison.InvariantCultureIgnoreCase)) { remaining = remaining.Value.Substring(3); } var segments = remaining.Value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); TrieNode currentNode; // The requested url is for the start page with no action // so just return the start page if (!segments.Any()) { trie.TryGetNode("/", out currentNode); } else { var requestPath = NormalizeRequestPath(remaining); // The normal behaviour is to load the page based on the incoming url trie.TryGetNode(requestPath, out currentNode); // Try to find the node without the last segment of the url and set the last segment as action if (currentNode == null) { action = segments.Last(); requestPath = string.Join("/", segments, 0, (segments.Length - 1)); trie.TryGetNode("/" + requestPath, out currentNode); } } if (currentNode == null) { return(null); } if (segments.Any()) { // We always need to check if the last segment is a valid action on the controller for the node we found string possibleAction = segments.Last(); if (_controllerMapper.ControllerHasAction(currentNode.ControllerName, possibleAction)) { action = possibleAction; } } if (!_controllerMapper.ControllerHasAction(currentNode.ControllerName, action)) { return(null); } return(new ResolveResult(currentNode, currentNode.ControllerName, action)); }
/// <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); }