Skip to content

Commit

Permalink
Merge pull request #222 from uhaciogullari/sitemap-part-url-fix
Browse files Browse the repository at this point in the history
Sitemap page links are broken when application is hosted as sub application
  • Loading branch information
NightOwl888 committed Aug 30, 2013
2 parents 051a581 + 33a935d commit d14fbba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public XmlSiteMapResult(
IEnumerable<string> siteMapCacheKeys,
string baseUrl,
string siteMapUrlTemplate,
ISiteMapLoader siteMapLoader)
ISiteMapLoader siteMapLoader,
IUrlPath urlPath)
{
if (siteMapLoader == null)
throw new ArgumentNullException("siteMapLoader");
Expand All @@ -33,10 +34,13 @@ public XmlSiteMapResult(
this.SiteMapCacheKeys = siteMapCacheKeys;
this.BaseUrl = baseUrl;
this.SiteMapUrlTemplate = siteMapUrlTemplate;
this.siteMapLoader = siteMapLoader;
this.siteMapLoader = siteMapLoader;
this.urlPath = urlPath;
}

private readonly ISiteMapLoader siteMapLoader;
private readonly ISiteMapLoader siteMapLoader;

private readonly IUrlPath urlPath;

/// <summary>
/// Maximal number of links per sitemap file.
Expand Down Expand Up @@ -106,7 +110,7 @@ protected virtual void ExecuteSitemapIndexResult(ControllerContext context, IEnu

// Generate sitemap sitemapindex
var sitemapIndex = new XElement(Ns + "sitemapindex");
sitemapIndex.Add(GenerateSiteMapIndexElements(Convert.ToInt32(numPages), BaseUrl, SiteMapUrlTemplate).ToArray());
sitemapIndex.Add(GenerateSiteMapIndexElements(Convert.ToInt32(numPages), SiteMapUrlTemplate).ToArray());

// Generate sitemap
var xmlSiteMap = new XDocument(
Expand Down Expand Up @@ -204,22 +208,21 @@ public override void ExecuteResult(ControllerContext context)
// Sitemap file for all links
ExecuteSitemapResult(context, flattenedHierarchy, flattenedHierarchyCount, 1);
}
}

/// <summary>
/// Generates the sitemap index elements.
/// </summary>
/// <param name="numPages">The number of pages.</param>
/// <param name="url">The URL.</param>
/// <param name="siteMapUrlTemplate">The site map URL template.</param>
/// <returns>The sitemap index elements.</returns>
protected virtual IEnumerable<XElement> GenerateSiteMapIndexElements(int numPages, string url, string siteMapUrlTemplate)
}

/// <summary>
/// Generates the sitemap index elements.
/// </summary>
/// <param name="numPages">The number of pages.</param>
/// <param name="siteMapUrlTemplate">The site map URL template.</param>
/// <returns>The sitemap index elements.</returns>
protected virtual IEnumerable<XElement> GenerateSiteMapIndexElements(int numPages, string siteMapUrlTemplate)
{
// Generate elements
for (int i = 1; i <= numPages; i++)
{
yield return new XElement(Ns + "sitemap",
new XElement(Ns + "loc", url + "/" + siteMapUrlTemplate.Replace("{page}", i.ToString())));
for (int i = 1; i <= numPages; i++)
{
var pageUrl = urlPath.MakeRelativeUrlAbsolute("~/" + siteMapUrlTemplate.Replace("{page}", i.ToString()));
yield return new XElement(Ns + "sitemap", new XElement(Ns + "loc", pageUrl));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public virtual ActionResult Create()
this.DefaultSiteMapCacheKeys,
this.DefaultBaseUrl,
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader);
this.siteMapLoader,
this.urlPath);
}

public ActionResult Create(int page)
Expand All @@ -50,8 +51,9 @@ public ActionResult Create(int page)
this.DefaultRootNode,
this.DefaultSiteMapCacheKeys,
this.DefaultBaseUrl,
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader);
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader,
this.urlPath);
}

public virtual ActionResult Create(IEnumerable<string> siteMapCacheKeys)
Expand All @@ -61,8 +63,9 @@ public virtual ActionResult Create(IEnumerable<string> siteMapCacheKeys)
this.DefaultRootNode,
siteMapCacheKeys,
this.DefaultBaseUrl,
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader);
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader,
this.urlPath);
}

public ActionResult Create(int page, IEnumerable<string> siteMapCacheKeys)
Expand All @@ -72,8 +75,9 @@ public ActionResult Create(int page, IEnumerable<string> siteMapCacheKeys)
this.DefaultRootNode,
siteMapCacheKeys,
this.DefaultBaseUrl,
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader);
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader,
this.urlPath);
}

public virtual ActionResult Create(IEnumerable<string> siteMapCacheKeys, string baseUrl, string siteMapUrlTemplate)
Expand All @@ -83,8 +87,9 @@ public virtual ActionResult Create(IEnumerable<string> siteMapCacheKeys, string
this.DefaultRootNode,
siteMapCacheKeys,
baseUrl,
siteMapUrlTemplate,
this.siteMapLoader);
siteMapUrlTemplate,
this.siteMapLoader,
this.urlPath);
}

public ActionResult Create(int page, IEnumerable<string> siteMapCacheKeys, string baseUrl, string siteMapUrlTemplate)
Expand All @@ -94,8 +99,9 @@ public ActionResult Create(int page, IEnumerable<string> siteMapCacheKeys, strin
this.DefaultRootNode,
siteMapCacheKeys,
baseUrl,
siteMapUrlTemplate,
this.siteMapLoader);
siteMapUrlTemplate,
this.siteMapLoader,
this.urlPath);
}

public virtual ActionResult Create(ISiteMapNode rootNode)
Expand All @@ -105,8 +111,9 @@ public virtual ActionResult Create(ISiteMapNode rootNode)
rootNode,
this.DefaultSiteMapCacheKeys,
this.DefaultBaseUrl,
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader);
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader,
this.urlPath);
}

public ActionResult Create(int page, ISiteMapNode rootNode)
Expand All @@ -116,8 +123,9 @@ public ActionResult Create(int page, ISiteMapNode rootNode)
rootNode,
this.DefaultSiteMapCacheKeys,
this.DefaultBaseUrl,
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader);
this.DefaultSiteMapUrlTemplate,
this.siteMapLoader,
this.urlPath);
}

public virtual ActionResult Create(ISiteMapNode rootNode, string baseUrl, string siteMapUrlTemplate)
Expand All @@ -127,8 +135,9 @@ public virtual ActionResult Create(ISiteMapNode rootNode, string baseUrl, string
rootNode,
this.DefaultSiteMapCacheKeys,
baseUrl,
siteMapUrlTemplate,
this.siteMapLoader);
siteMapUrlTemplate,
this.siteMapLoader,
this.urlPath);
}

public ActionResult Create(int page, ISiteMapNode rootNode, string baseUrl, string siteMapUrlTemplate)
Expand All @@ -138,8 +147,9 @@ public ActionResult Create(int page, ISiteMapNode rootNode, string baseUrl, stri
rootNode,
this.DefaultSiteMapCacheKeys,
baseUrl,
siteMapUrlTemplate,
this.siteMapLoader);
siteMapUrlTemplate,
this.siteMapLoader,
this.urlPath);
}

#endregion
Expand Down

0 comments on commit d14fbba

Please sign in to comment.