예제 #1
0
        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 page repository
            _repository = _container.GetInstance<IPageRepository>();
            // The requested url is for the start page with no action
            if (string.IsNullOrEmpty(virtualUrl) || string.Equals(virtualUrl,"/")) {
                _pageModel = _repository.SingleOrDefault<IPageModel>(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 = _repository.GetPageByUrl<IPageModel>(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 = _repository.GetPageByUrl<IPageModel>(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 = _repository.SingleOrDefault<IPageModel>(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;
        }