Skip to content

Commit

Permalink
Handle null Urls from unpublished nodes (#2)
Browse files Browse the repository at this point in the history
* 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 👍
  • Loading branch information
amitch17 authored and christopherrobinson committed Sep 24, 2019
1 parent a461bbe commit 8a09c96
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions VirtualNodes/VirtualNodesHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static int GetMaxNodeNameNumbering(string potentialDuplicateName, string
/// <returns>True if it is a virtual node</returns>
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))
Expand Down
8 changes: 4 additions & 4 deletions VirtualNodes/VirtualNodesUrlProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ 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.
var hostPart = "";

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);
Expand Down Expand Up @@ -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] = "";
}
Expand Down

0 comments on commit 8a09c96

Please sign in to comment.