/// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedRequest</c>.
        /// </summary>
        /// <param name="frequest">The <c>PublishedRequest</c>.</param>
        /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
        /// <remarks>If successful, also assigns the template.</remarks>
        public override bool TryFindContent(PublishedRequest frequest)
        {
            IPublishedContent node = null;
            var path = frequest.Uri.GetAbsolutePathDecoded();

            if (frequest.HasDomain)
            {
                path = DomainHelper.PathRelativeToDomain(frequest.Domain.Uri, path);
            }

            // no template if "/"
            if (path == "/")
            {
                Logger.Debug <ContentFinderByUrlAndTemplate>("No template in path '/'");
                return(false);
            }

            // look for template in last position
            var pos           = path.LastIndexOf('/');
            var templateAlias = path.Substring(pos + 1);

            path = pos == 0 ? "/" : path.Substring(0, pos);

            var template = _fileService.GetTemplate(templateAlias);

            if (template == null)
            {
                Logger.Debug <ContentFinderByUrlAndTemplate>("Not a valid template: '{TemplateAlias}'", templateAlias);
                return(false);
            }

            Logger.Debug <ContentFinderByUrlAndTemplate>("Valid template: '{TemplateAlias}'", templateAlias);

            // look for node corresponding to the rest of the route
            var route = frequest.HasDomain ? (frequest.Domain.ContentId + path) : path;

            node = FindContent(frequest, route); // also assigns to published request

            if (node == null)
            {
                Logger.Debug <ContentFinderByUrlAndTemplate>("Not a valid route to node: '{Route}'", route);
                return(false);
            }

            // IsAllowedTemplate deals both with DisableAlternativeTemplates and ValidateAlternativeTemplates settings
            if (!node.IsAllowedTemplate(template.Id))
            {
                Logger.Warn <ContentFinderByUrlAndTemplate>("Alternative template '{TemplateAlias}' is not allowed on node {NodeId}.", template.Alias, node.Id);
                frequest.PublishedContent = null; // clear
                return(false);
            }

            // got it
            frequest.TemplateModel = template;
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
        /// </summary>
        /// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>
        /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
        /// <remarks>If successful, also assigns the template.</remarks>
        public override bool TryFindContent(PublishedContentRequest docRequest)
        {
            const string tracePrefix = "ContentFinderByNiceUrlAndTemplate: ";

            IPublishedContent node = null;
            string            path = docRequest.Uri.GetAbsolutePathDecoded();

            if (docRequest.HasDomain)
            {
                path = DomainHelper.PathRelativeToDomain(docRequest.DomainUri, path);
            }

            if (path != "/") // no template if "/"
            {
                var pos           = path.LastIndexOf('/');
                var templateAlias = path.Substring(pos + 1);
                path = pos == 0 ? "/" : path.Substring(0, pos);

                var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias);
                if (template != null)
                {
                    LogHelper.Debug <ContentFinderByNiceUrlAndTemplate>("Valid template: \"{0}\"", () => templateAlias);

                    var route = docRequest.HasDomain ? (docRequest.Domain.RootNodeId.ToString() + path) : path;
                    node = FindContent(docRequest, route);

                    if (node.IsAllowedTemplate(template.Id))
                    {
                        docRequest.TemplateModel = template;
                    }
                    else
                    {
                        LogHelper.Warn <ContentFinderByNiceUrlAndTemplate>("Configuration settings prevent template \"{0}\" from showing for node \"{1}\"", () => templateAlias, () => node.Id);
                        docRequest.PublishedContent = null;
                        node = null;
                    }
                }
                else
                {
                    LogHelper.Debug <ContentFinderByNiceUrlAndTemplate>("Not a valid template: \"{0}\"", () => templateAlias);
                }
            }
            else
            {
                LogHelper.Debug <ContentFinderByNiceUrlAndTemplate>("No template in path \"/\"");
            }

            return(node != null);
        }
Esempio n. 3
0
        /// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
        /// </summary>
        /// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>
        /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
        public virtual bool TrySetDocument(PublishedContentRequest docRequest)
        {
            string route;

            if (docRequest.HasDomain)
            {
                route = docRequest.Domain.RootNodeId.ToString() + DomainHelper.PathRelativeToDomain(docRequest.DomainUri, docRequest.Uri.GetAbsolutePathDecoded());
            }
            else
            {
                route = docRequest.Uri.GetAbsolutePathDecoded();
            }

            var node = LookupDocumentNode(docRequest, route);

            return(node != null);
        }
Esempio n. 4
0
        /// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
        /// </summary>
        /// <param name="frequest">The <c>PublishedContentRequest</c>.</param>
        /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
        public virtual bool TryFindContent(PublishedRequest frequest)
        {
            string route;

            if (frequest.HasDomain)
            {
                route = frequest.Domain.ContentId + DomainHelper.PathRelativeToDomain(frequest.Domain.Uri, frequest.Uri.GetAbsolutePathDecoded());
            }
            else
            {
                route = frequest.Uri.GetAbsolutePathDecoded();
            }

            var node = FindContent(frequest, route);

            return(node != null);
        }
        /// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
        /// </summary>
        /// <param name="contentRequest">The <c>PublishedContentRequest</c>.</param>
        /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
        /// <remarks>Optionally, can also assign the template or anything else on the document request, although that is not required.</remarks>
        public bool TryFindContent(PublishedContentRequest contentRequest)
        {
            var route = contentRequest.HasDomain
                ? contentRequest.UmbracoDomain.RootContentId + DomainHelper.PathRelativeToDomain(contentRequest.DomainUri, contentRequest.Uri.GetAbsolutePathDecoded())
                : contentRequest.Uri.GetAbsolutePathDecoded();

            var service     = contentRequest.RoutingContext.UmbracoContext.Application.Services.RedirectUrlService;
            var redirectUrl = service.GetMostRecentRedirectUrl(route);

            if (redirectUrl == null)
            {
                LogHelper.Debug <ContentFinderByRedirectUrl>("No match for route: \"{0}\".", () => route);
                return(false);
            }

            var content = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(redirectUrl.ContentId);
            var url     = content == null ? "#" : content.Url;

            if (url.StartsWith("#"))
            {
                LogHelper.Debug <ContentFinderByRedirectUrl>("Route \"{0}\" matches content {1} which has no url.",
                                                             () => route, () => redirectUrl.ContentId);
                return(false);
            }

            // Apending any querystring from the incoming request to the redirect url.
            url = string.IsNullOrEmpty(contentRequest.Uri.Query) ? url : url + contentRequest.Uri.Query;

            LogHelper.Debug <ContentFinderByRedirectUrl>("Route \"{0}\" matches content {1} with url \"{2}\", redirecting.",
                                                         () => route, () => content.Id, () => url);

            // From: http://stackoverflow.com/a/22468386/5018
            // See http://issues.umbraco.org/issue/U4-8361#comment=67-30532
            // Setting automatic 301 redirects to not be cached because browsers cache these very aggressively which then leads
            // to problems if you rename a page back to it's original name or create a new page with the original name
            contentRequest.Cacheability    = HttpCacheability.NoCache;
            contentRequest.CacheExtensions = new List <string> {
                "no-store, must-revalidate"
            };
            contentRequest.Headers = new Dictionary <string, string> {
                { "Pragma", "no-cache" }, { "Expires", "0" }
            };

            contentRequest.SetRedirectPermanent(url);
            return(true);
        }
        /// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
        /// </summary>
        /// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>
        /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
        /// <remarks>If successful, also assigns the template.</remarks>
        public override bool TrySetDocument(PublishedContentRequest docRequest)
        {
            IPublishedContent node = null;
            string            path = docRequest.Uri.GetAbsolutePathDecoded();

            if (docRequest.HasDomain)
            {
                path = DomainHelper.PathRelativeToDomain(docRequest.DomainUri, path);
            }

            if (path != "/")             // no template if "/"
            {
                var pos           = path.LastIndexOf('/');
                var templateAlias = path.Substring(pos + 1);
                path = pos == 0 ? "/" : path.Substring(0, pos);

                //TODO: We need to check if the altTemplate is for MVC or not, though I'm not exactly sure how the best
                // way to do that would be since the template is just an alias and if we are not having a flag on the
                // doc type for rendering engine and basing it only on template name, then how would we handle this?

                var template = Template.GetByAlias(templateAlias);
                if (template != null)
                {
                    LogHelper.Debug <LookupByNiceUrlAndTemplate>("Valid template: \"{0}\"", () => templateAlias);

                    var route = docRequest.HasDomain ? (docRequest.Domain.RootNodeId.ToString() + path) : path;
                    node = LookupDocumentNode(docRequest, route);

                    if (node != null)
                    {
                        docRequest.Template = template;
                    }
                }
                else
                {
                    LogHelper.Debug <LookupByNiceUrlAndTemplate>("Not a valid template: \"{0}\"", () => templateAlias);
                }
            }
            else
            {
                LogHelper.Debug <LookupByNiceUrlAndTemplate>("No template in path \"/\"");
            }

            return(node != null);
        }
        /// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
        /// </summary>
        /// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>
        /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
        /// <remarks>If successful, also assigns the template.</remarks>
        public override bool TryFindContent(PublishedContentRequest docRequest)
        {
            IPublishedContent node = null;
            string            path = docRequest.Uri.GetAbsolutePathDecoded();

            if (docRequest.HasDomain)
            {
                path = DomainHelper.PathRelativeToDomain(docRequest.DomainUri, path);
            }

            if (path != "/") // no template if "/"
            {
                var pos           = path.LastIndexOf('/');
                var templateAlias = path.Substring(pos + 1);
                path = pos == 0 ? "/" : path.Substring(0, pos);

                var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias);
                if (template != null)
                {
                    LogHelper.Debug <ContentFinderByNiceUrlAndTemplate>("Valid template: \"{0}\"", () => templateAlias);

                    var route = docRequest.HasDomain ? (docRequest.Domain.RootNodeId.ToString() + path) : path;
                    node = FindContent(docRequest, route);

                    if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false && node != null)
                    {
                        docRequest.TemplateModel = template;
                    }
                }
                else
                {
                    LogHelper.Debug <ContentFinderByNiceUrlAndTemplate>("Not a valid template: \"{0}\"", () => templateAlias);
                }
            }
            else
            {
                LogHelper.Debug <ContentFinderByNiceUrlAndTemplate>("No template in path \"/\"");
            }

            return(node != null);
        }