Skip to content

Commit

Permalink
Fixes issue described in #345, XML Sitemap excludes all URLs with a c…
Browse files Browse the repository at this point in the history
…anonical URL configured rather than checking whether the URL is a canonical URL.
  • Loading branch information
NightOwl888 committed Nov 2, 2014
1 parent e174e8e commit c34f077
Showing 1 changed file with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
Expand Down Expand Up @@ -318,14 +319,56 @@ protected virtual IEnumerable<ISiteMapNode> FlattenHierarchy(ISiteMapNode starti
/// <returns><b>true</b> if the current node should be rendered; otherwise<b>false</b>.</returns>
protected virtual bool ShouldNodeRender(ISiteMapNode node, ControllerContext context)
{

return node.Clickable &&
node.IsVisible(SourceMetadata) &&
!node.HasExternalUrl(context.HttpContext) &&
string.IsNullOrEmpty(node.CanonicalUrl) &&
this.IsCanonicalUrl(node, context.HttpContext) &&
!node.HasNoIndexAndNoFollow &&
!this.IsDuplicateUrl(node);
}

/// <summary>
/// Determines whether the URL of the current node is a canonical node.
/// </summary>
/// <param name="node">The node</param>
/// <returns><c>true</c> if the node's URL is canonical; otherwise<c>false</c>.</returns>
protected virtual bool IsCanonicalUrl(ISiteMapNode node, HttpContextBase context)
{
var canonicalUrl = node.CanonicalUrl;
if (string.IsNullOrEmpty(canonicalUrl))
{
return true;
}

string absoluteUrl = string.Empty;

if (string.IsNullOrEmpty(node.Protocol) && string.IsNullOrEmpty(node.HostName))
{
var mvcContextFactory = new MvcContextFactory();

// Use the HTTP protocol to force an absolute URL to compare with if no protocol was provided.
var protocol = string.IsNullOrEmpty(node.Protocol) ? Uri.UriSchemeHttp : node.Protocol;

// Create a URI with the home page and no query string values.
var uri = new Uri(context.Request.Url, "/");

// Create a TextWriter with null stream as a backing stream
// which doesn't consume resources
using (var nullWriter = new StreamWriter(Stream.Null))
{
var newContext = mvcContextFactory.CreateHttpContext(node, uri, nullWriter);
absoluteUrl = this.urlPath.ResolveUrl(node.Url, protocol, node.HostName, newContext);
}
}
else
{
absoluteUrl = node.Url;
}

return absoluteUrl.Equals(node.CanonicalUrl, StringComparison.Ordinal) || absoluteUrl.Equals("#");
}

/// <summary>
/// Determines whether the URL is already included in the sitemap.
/// </summary>
Expand Down

0 comments on commit c34f077

Please sign in to comment.