public override bool TryFindContent(PublishedContentRequest docRequest)
        {
            string route = string.Empty;

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

            var       node          = FindContent(docRequest, route);
            string    templateAlias = GetTemplateAliasByContentAccept(docRequest);
            ITemplate template      = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias);

            if (template != null)
            {
                LogHelper.Debug <ContentFinderByNiceUrlWithContentAccept>("Valid template: \"{0}\"", () => templateAlias);
                if (node != null)
                {
                    docRequest.SetTemplate(template);
                }
            }
            else
            {
                LogHelper.Debug <ContentFinderByNiceUrlWithContentAccept>("Not a valid template: \"{0}\"", () => templateAlias);
            }
            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>If successful, also assigns the template.</remarks>
        public override bool TryFindContent(PublishedContentRequest contentRequest)
        {
            IPublishedContent node = null;
            string            path = contentRequest.Uri.GetAbsolutePathDecoded();

            if (contentRequest.HasDomain)
            {
                path = DomainHelper.PathRelativeToDomain(contentRequest.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 <ContentFinderByUrlAliasAndTemplate>("Valid template: \"{0}\"", () => templateAlias);

                    node = FindContentByAlias(
                        contentRequest.RoutingContext.UmbracoContext.ContentCache,
                        contentRequest.HasDomain && contentRequest.UmbracoDomain.HasIdentity ?
                        contentRequest.UmbracoDomain.RootContentId.Value :
                        0,
                        contentRequest.HasDomain ?
                        DomainHelper.PathRelativeToDomain(contentRequest.DomainUri, contentRequest.Uri.GetAbsolutePathDecoded()) :
                        contentRequest.Uri.GetAbsolutePathDecoded());

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

            return(node != null);
        }
Example #3
0
        private void SetTemplate(PublishedContentRequest contentRequest, string template, bool forceTemplate)
        {
            if (contentRequest.PublishedContent == null)
            {
                return;
            }

            if (contentRequest.TemplateAlias == null || string.IsNullOrWhiteSpace(contentRequest.TemplateAlias) || forceTemplate)
            {
                if (!string.IsNullOrWhiteSpace(template))
                {
                    var isValidTemplate = System.IO.File.Exists(HttpContext.Current.Server.MapPath(template));
                    if (!isValidTemplate)
                    {
                        return;
                    }

                    // Check whether it as an alias or a path
                    if (!template.Contains("/"))
                    {
                        contentRequest.TrySetTemplate(template);
                    }
                    else
                    {
                        contentRequest.SetTemplate(new Template(template, template, Guid.NewGuid().ToString().Replace("-", string.Empty)));
                    }

                    var requestUrl = contentRequest.Uri.OriginalString;

                    // Add the template to the cache in order to retrieve it from the Render MVC controller
                    var    cacheId           = string.Format(Constants.Cache.TemplateCacheIdPattern, requestUrl.ToLower().Trim());
                    string cacheDependencyId = GetNodeCacheDependency(contentRequest.PublishedContent.Id);
                    Helpers.CacheHelper.GetExistingOrAddToCacheSlidingExpiration(cacheId, 300, CacheItemPriority.NotRemovable,
                                                                                 () =>
                    {
                        return(template);
                    },
                                                                                 new[] { Constants.Config.ConfigFilePhysicalPath }, new[] { cacheDependencyId });
                }
            }
        }