Ejemplo n.º 1
0
        /// <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 = DomainUtilities.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, string>("Not a valid template: '{TemplateAlias}'", templateAlias);
                return(false);
            }

            Logger.Debug <ContentFinderByUrlAndTemplate, string>("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, string>("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, string, int>("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);
        }
Ejemplo n.º 2
0
        /// <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>Optionally, can also assign the template or anything else on the document request, although that is not required.</remarks>
        public bool TryFindContent(PublishedRequest frequest)
        {
            var route = frequest.HasDomain
                ? frequest.Domain.ContentId + DomainUtilities.PathRelativeToDomain(frequest.Domain.Uri, frequest.Uri.GetAbsolutePathDecoded())
                : frequest.Uri.GetAbsolutePathDecoded();



            var redirectUrl = _redirectUrlService.GetMostRecentRedirectUrl(route, frequest.Culture.Name);

            if (redirectUrl == null)
            {
                _logger.Debug <ContentFinderByRedirectUrl, string>("No match for route: {Route}", route);
                return(false);
            }

            var content = frequest.UmbracoContext.Content.GetById(redirectUrl.ContentId);
            var url     = content == null ? "#" : content.Url(redirectUrl.Culture);

            if (url.StartsWith("#"))
            {
                _logger.Debug <ContentFinderByRedirectUrl, string, int>("Route {Route} matches content {ContentId} which has no URL.", route, redirectUrl.ContentId);
                return(false);
            }

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

            _logger.Debug <ContentFinderByRedirectUrl, string, int, string>("Route {Route} matches content {ContentId} with url '{Url}', redirecting.", route, content.Id, url);
            frequest.SetRedirectPermanent(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
            frequest.Cacheability    = HttpCacheability.NoCache;
            frequest.CacheExtensions = new List <string> {
                "no-store, must-revalidate"
            };
            frequest.Headers = new Dictionary <string, string> {
                { "Pragma", "no-cache" }, { "Expires", "0" }
            };

            return(true);
        }
Ejemplo n.º 3
0
        /// <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>
        public virtual bool TryFindContent(PublishedRequest frequest)
        {
            string route;

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

            var node = FindContent(frequest, route);

            return(node != null);
        }