コード例 #1
0
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            // TODO handle urlGeneration
            var nodeValue = values[parameterName];

            if (nodeValue == null)
            {
                return(false);
            }

            string action            = null;
            var    currentContentUri = new Uri(ContentUri.BaseUri, nodeValue.ToString());
            var    currentContent    = _contentRepository.Get(currentContentUri);

            if (currentContent == null && currentContentUri != ContentUri.BaseUri)
            {
                action = currentContentUri.Segments.Last();

                currentContentUri = ContentUri.GetParent(currentContentUri);
                currentContent    = _contentRepository.Get(currentContentUri);
            }

            // TODO: also check if user is logged in and SHOULD see unpublished pages
            if (currentContent == null || (!currentContent.IsPublished && !httpContext.IsDebuggingEnabled))
            {
                return(false);
            }

            var layoutMetaData = _contentControllerResolver.Resolve(currentContent.GetType());

            if (layoutMetaData != null)
            {
                // TODO increase performance of this section
                if (action != null)
                {
                    var actionMethods = layoutMetaData.ControllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
                    var hasAction     = actionMethods.Any(m => m.Name.Equals(action, StringComparison.InvariantCultureIgnoreCase)); // this won't take ActionName attributes in consideration.

                    // controller doesn't have a matching action so we won't route through cms.
                    if (!hasAction)
                    {
                        return(false);
                    }
                }

                values["controller"] = layoutMetaData.ControllerName;
                values["action"]     = action ?? layoutMetaData.ActionName;
            }
            else
            {
                throw new InvalidOperationException(string.Format("Could not find suitable controller for content type \"{0}\". \r\nCreate a new controller named \"{1}Controller\", or use the [ContentController(typeof({1}))] attribute on a controller that should be used.", currentContent.Layout, currentContent.GetType().Name));
            }

            values[MvcContextExtensions.CURRENT_CONTENT_KEY] = currentContent;
            return(true);
        }
コード例 #2
0
        public override string Generate(IShortcodeContext context)
        {
            var sb = new StringBuilder();

            sb.AppendFormat("<ul class=\"{0}\">", ListCssClass);

            var contentRepository = EncelApplication.Configuration.ContentRepositoryFactory.Create();
            var currentContent    = HttpContext.Current.Request.RequestContext.GetCurrentContent();
            var contentUri        = Root != null?ContentUri.Parse(Root) : currentContent.ContentUri;

            IEnumerable <IContentData> children;

            if (Descendants)
            {
                children = contentRepository.GetDescendants(contentUri);
            }
            else
            {
                children = contentRepository.GetChildren(contentUri);
            }

            children = children.OrderBy(m => m.Published).AsEnumerable();

            if (IsSet("OfType"))
            {
                children = children.Where(m => m.Layout != null && m.Layout.Equals(OfType, StringComparison.InvariantCultureIgnoreCase));
            }

            if (Take.HasValue)
            {
                children = children.Take(Take.GetValueOrDefault());
            }

            foreach (var contentData in children)
            {
                sb.AppendFormat("<li class=\"{2}\"><a href=\"{0}\">{1}</a></li>", contentData.ContentUri.AbsolutePath, contentData.Title ?? contentData.Slug, ListItemCssClass);
            }

            sb.Append("</ul>");

            return(sb.ToString());
        }