diff --git a/src/Umbraco.Core/Routing/PublishedRequest.cs b/src/Umbraco.Core/Routing/PublishedRequest.cs index badfb27dd2..e42211da49 100644 --- a/src/Umbraco.Core/Routing/PublishedRequest.cs +++ b/src/Umbraco.Core/Routing/PublishedRequest.cs @@ -13,7 +13,7 @@ namespace Umbraco.Web.Routing /// /// Initializes a new instance of the class. /// - public PublishedRequest(Uri uri, IPublishedContent publishedContent, bool isInternalRedirect, ITemplate template, DomainAndUri domain, string culture, string redirectUrl, int? responseStatusCode, IReadOnlyList cacheExtensions, IReadOnlyDictionary headers, bool cacheabilityNoCache, bool ignorePublishedContentCollisions) + public PublishedRequest(Uri uri, IPublishedContent publishedContent, bool isInternalRedirect, ITemplate template, DomainAndUri domain, string culture, string redirectUrl, int? responseStatusCode, IReadOnlyList cacheExtensions, IReadOnlyDictionary headers, bool setNoCacheHeader, bool ignorePublishedContentCollisions) { Uri = uri ?? throw new ArgumentNullException(nameof(uri)); PublishedContent = publishedContent; @@ -25,7 +25,7 @@ namespace Umbraco.Web.Routing ResponseStatusCode = responseStatusCode; CacheExtensions = cacheExtensions; Headers = headers; - SetNoCacheHeader = cacheabilityNoCache; + SetNoCacheHeader = setNoCacheHeader; IgnorePublishedContentCollisions = ignorePublishedContentCollisions; } diff --git a/src/Umbraco.Core/Routing/PublishedRouter.cs b/src/Umbraco.Core/Routing/PublishedRouter.cs index 4d7f0eef82..4e0cda4041 100644 --- a/src/Umbraco.Core/Routing/PublishedRouter.cs +++ b/src/Umbraco.Core/Routing/PublishedRouter.cs @@ -665,7 +665,14 @@ namespace Umbraco.Web.Routing var templateId = request.PublishedContent.TemplateId; ITemplate template = GetTemplate(templateId); request.SetTemplate(template); - _logger.LogDebug("FindTemplate: Running with template id={TemplateId} alias={TemplateAlias}", template.Id, template.Alias); + if (template != null) + { + _logger.LogDebug("FindTemplate: Running with template id={TemplateId} alias={TemplateAlias}", template.Id, template.Alias); + } + else + { + _logger.LogWarning("FindTemplate: Could not find template with id {TemplateId}", templateId); + } } else { diff --git a/src/Umbraco.Core/Templates/IUmbracoComponentRenderer.cs b/src/Umbraco.Core/Templates/IUmbracoComponentRenderer.cs index 5444ef3f96..fcfdd815f6 100644 --- a/src/Umbraco.Core/Templates/IUmbracoComponentRenderer.cs +++ b/src/Umbraco.Core/Templates/IUmbracoComponentRenderer.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System.Collections.Generic; +using System.Threading.Tasks; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Strings; @@ -12,35 +13,31 @@ namespace Umbraco.Core.Templates /// /// Renders the template for the specified pageId and an optional altTemplateId /// - /// + /// The content id /// If not specified, will use the template assigned to the node - /// - IHtmlEncodedString RenderTemplate(int contentId, int? altTemplateId = null); + Task RenderTemplateAsync(int contentId, int? altTemplateId = null); /// /// Renders the macro with the specified alias. /// - /// + /// The content id /// The alias. - /// IHtmlEncodedString RenderMacro(int contentId, string alias); /// /// Renders the macro with the specified alias, passing in the specified parameters. /// - /// + /// The content id /// The alias. /// The parameters. - /// IHtmlEncodedString RenderMacro(int contentId, string alias, object parameters); /// /// Renders the macro with the specified alias, passing in the specified parameters. /// - /// + /// The content id /// The alias. /// The parameters. - /// IHtmlEncodedString RenderMacro(int contentId, string alias, IDictionary parameters); /// diff --git a/src/Umbraco.Core/Templates/UmbracoComponentRenderer.cs b/src/Umbraco.Core/Templates/UmbracoComponentRenderer.cs index 53e856ced4..d0b6972bc3 100644 --- a/src/Umbraco.Core/Templates/UmbracoComponentRenderer.cs +++ b/src/Umbraco.Core/Templates/UmbracoComponentRenderer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.IO; using Umbraco.Web.Templates; @@ -8,6 +8,7 @@ using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Strings; using Umbraco.Web; using Umbraco.Web.Macros; +using System.Threading.Tasks; namespace Umbraco.Core.Templates { @@ -24,6 +25,9 @@ namespace Umbraco.Core.Templates private readonly IMacroRenderer _macroRenderer; private readonly ITemplateRenderer _templateRenderer; + /// + /// Initializes a new instance of the class. + /// public UmbracoComponentRenderer(IUmbracoContextAccessor umbracoContextAccessor, IMacroRenderer macroRenderer, ITemplateRenderer templateRenderer) { _umbracoContextAccessor = umbracoContextAccessor; @@ -31,76 +35,55 @@ namespace Umbraco.Core.Templates _templateRenderer = templateRenderer ?? throw new ArgumentNullException(nameof(templateRenderer)); } - /// - /// Renders the template for the specified pageId and an optional altTemplateId - /// - /// - /// If not specified, will use the template assigned to the node - /// - public IHtmlEncodedString RenderTemplate(int contentId, int? altTemplateId = null) + /// + public async Task RenderTemplateAsync(int contentId, int? altTemplateId = null) { using (var sw = new StringWriter()) { try { - _templateRenderer.RenderAsync(contentId, altTemplateId, sw); + await _templateRenderer.RenderAsync(contentId, altTemplateId, sw); } catch (Exception ex) { sw.Write("", contentId, ex); } + return new HtmlEncodedString(sw.ToString()); } } - /// - /// Renders the macro with the specified alias. - /// - /// - /// The alias. - /// - public IHtmlEncodedString RenderMacro(int contentId, string alias) - { - return RenderMacro(contentId, alias, new { }); - } + /// + public IHtmlEncodedString RenderMacro(int contentId, string alias) => RenderMacro(contentId, alias, new { }); - /// - /// Renders the macro with the specified alias, passing in the specified parameters. - /// - /// - /// The alias. - /// The parameters. - /// - public IHtmlEncodedString RenderMacro(int contentId, string alias, object parameters) - { - return RenderMacro(contentId, alias, parameters?.ToDictionary()); - } + /// + public IHtmlEncodedString RenderMacro(int contentId, string alias, object parameters) => RenderMacro(contentId, alias, parameters?.ToDictionary()); - /// - /// Renders the macro with the specified alias, passing in the specified parameters. - /// - /// - /// The alias. - /// The parameters. - /// + /// public IHtmlEncodedString RenderMacro(int contentId, string alias, IDictionary parameters) { if (contentId == default) + { throw new ArgumentException("Invalid content id " + contentId); + } var content = _umbracoContextAccessor.UmbracoContext.Content?.GetById(contentId); if (content == null) + { throw new InvalidOperationException("Cannot render a macro, no content found by id " + contentId); + } return RenderMacro(content, alias, parameters); } - + /// public IHtmlEncodedString RenderMacroForContent(IPublishedContent content, string alias, IDictionary parameters) { if(content == null) + { throw new InvalidOperationException("Cannot render a macro, IPublishedContent is null"); + } return RenderMacro(content, alias, parameters); } @@ -108,16 +91,15 @@ namespace Umbraco.Core.Templates /// /// Renders the macro with the specified alias, passing in the specified parameters. /// - /// The macro alias. - /// The parameters. - /// The content used for macro rendering - /// private IHtmlEncodedString RenderMacro(IPublishedContent content, string alias, IDictionary parameters) { - if (content == null) throw new ArgumentNullException(nameof(content)); + if (content == null) + { + throw new ArgumentNullException(nameof(content)); + } // TODO: We are doing at ToLower here because for some insane reason the UpdateMacroModel method looks for a lower case match. the whole macro concept needs to be rewritten. - //NOTE: the value could have HTML encoded values, so we need to deal with that + // NOTE: the value could have HTML encoded values, so we need to deal with that var macroProps = parameters?.ToDictionary( x => x.Key.ToLowerInvariant(), i => (i.Value is string) ? WebUtility.HtmlDecode(i.Value.ToString()) : i.Value); diff --git a/src/Umbraco.Web.Website/UmbracoHelper.cs b/src/Umbraco.Web.Website/UmbracoHelper.cs index d44ca4e5fe..ed6d5d36b0 100644 --- a/src/Umbraco.Web.Website/UmbracoHelper.cs +++ b/src/Umbraco.Web.Website/UmbracoHelper.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Xml.XPath; using Umbraco.Core; @@ -7,6 +7,7 @@ using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Templates; using Umbraco.Core.Strings; using Umbraco.Core.Xml; +using System.Threading.Tasks; namespace Umbraco.Web.Website { @@ -95,13 +96,10 @@ namespace Umbraco.Web.Website /// /// Renders the template for the specified pageId and an optional altTemplateId /// - /// + /// The content id /// If not specified, will use the template assigned to the node - /// - public IHtmlEncodedString RenderTemplate(int contentId, int? altTemplateId = null) - { - return _componentRenderer.RenderTemplate(contentId, altTemplateId); - } + public async Task RenderTemplateAsync(int contentId, int? altTemplateId = null) + => await _componentRenderer.RenderTemplateAsync(contentId, altTemplateId); #region RenderMacro diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index 7e00f5bd68..e026dadfa7 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Web; @@ -12,6 +12,7 @@ using Umbraco.Core.Xml; using Umbraco.Web.Mvc; using Microsoft.Extensions.Logging; using Umbraco.Web.Security; +using System.Threading.Tasks; namespace Umbraco.Web { @@ -104,13 +105,10 @@ namespace Umbraco.Web /// /// Renders the template for the specified pageId and an optional altTemplateId /// - /// + /// The content id /// If not specified, will use the template assigned to the node - /// - public IHtmlEncodedString RenderTemplate(int contentId, int? altTemplateId = null) - { - return _componentRenderer.RenderTemplate(contentId, altTemplateId); - } + public async Task RenderTemplateAsync(int contentId, int? altTemplateId = null) + => await _componentRenderer.RenderTemplateAsync(contentId, altTemplateId); #region RenderMacro