From 8a09c96f730f3bee36036c2fa7f9b6bf816dbd5e Mon Sep 17 00:00:00 2001 From: Alan Mitchell Date: Tue, 24 Sep 2019 15:14:59 +0100 Subject: [PATCH] Handle null Urls from unpublished nodes (#2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Handle null Urls from unpublished nodes Fixes error that occurs if Umbraco is in preview mode. An unpublished document will have a null url, which needs to be handled as a blank contribution to the url path. * Update VirtualNodesHelpers.cs Additional safety checks for null values * Update VirtualNodesUrlProvider.cs Additional safety checks. Needed in case content is not found in the cache (unpublished). Thanks to @amitch17 for these bug fixes 👍 --- VirtualNodes/VirtualNodesHelpers.cs | 2 ++ VirtualNodes/VirtualNodesUrlProvider.cs | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/VirtualNodes/VirtualNodesHelpers.cs b/VirtualNodes/VirtualNodesHelpers.cs index d81bf62..d1f7af5 100644 --- a/VirtualNodes/VirtualNodesHelpers.cs +++ b/VirtualNodes/VirtualNodesHelpers.cs @@ -46,6 +46,8 @@ public static int GetMaxNodeNameNumbering(string potentialDuplicateName, string /// True if it is a virtual node public static bool IsVirtualNode(this IPublishedContent item) { + if (item == null) return false; + foreach (string rule in VirtualNodesRuleManager.Instance.Rules) { if (MatchContentTypeAlias(item.ContentType.Alias, rule)) diff --git a/VirtualNodes/VirtualNodesUrlProvider.cs b/VirtualNodes/VirtualNodesUrlProvider.cs index 9be98d8..5be0253 100644 --- a/VirtualNodes/VirtualNodesUrlProvider.cs +++ b/VirtualNodes/VirtualNodesUrlProvider.cs @@ -69,8 +69,8 @@ private UrlInfo ConstructUrl(UmbracoContext umbracoContext, IPublishedContent co // DO NOT USE THIS - RECURSES: string url = content.Url; // https://our.umbraco.org/forum/developers/extending-umbraco/73533-custom-url-provider-stackoverflowerror // https://our.umbraco.org/forum/developers/extending-umbraco/66741-iurlprovider-cannot-evaluate-expression-because-the-current-thread-is-in-a-stack-overflow-state - var url = base.GetUrl(umbracoContext, content, mode, culture, current); - var urlText = url.Text; + UrlInfo url = base.GetUrl(umbracoContext, content, mode, culture, current); + var urlText = url == null ? "" : url.Text; // If we come from an absolute URL, strip the host part and keep it so that we can append // it again when returing the URL. @@ -78,7 +78,7 @@ private UrlInfo ConstructUrl(UmbracoContext umbracoContext, IPublishedContent co if (urlText.StartsWith("http")) { - var uri = new Uri(url.Text); + var uri = new Uri(urlText); urlText = urlText.Replace(uri.GetLeftPart(UriPartial.Authority), ""); hostPart = uri.GetLeftPart(UriPartial.Authority); @@ -107,7 +107,7 @@ private UrlInfo ConstructUrl(UmbracoContext umbracoContext, IPublishedContent co var currentItem = umbracoContext.Content.GetById(int.Parse(pathIds[i])); // Omit any virtual node unless it's leaf level (we still need this otherwise it will be pointing to parent's URL) - if (currentItem.IsVirtualNode() && i > 0) + if (currentItem != null && currentItem.IsVirtualNode() && i > 0) { urlParts[i] = ""; }