From 33a935d5a6d1df76c4003f7fccfc9eaf6fda6b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ufuk=20Hac=C4=B1o=C4=9Fullar=C4=B1?= Date: Thu, 29 Aug 2013 12:46:01 +0300 Subject: [PATCH] Fixed sitemap page URLs in sitemap index --- .../Web/Mvc/XmlSiteMapResult.cs | 39 ++++++++------- .../Web/Mvc/XmlSiteMapResultFactory.cs | 48 +++++++++++-------- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/XmlSiteMapResult.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/XmlSiteMapResult.cs index 84af5080..31470fdc 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/XmlSiteMapResult.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/XmlSiteMapResult.cs @@ -22,7 +22,8 @@ public XmlSiteMapResult( IEnumerable siteMapCacheKeys, string baseUrl, string siteMapUrlTemplate, - ISiteMapLoader siteMapLoader) + ISiteMapLoader siteMapLoader, + IUrlPath urlPath) { if (siteMapLoader == null) throw new ArgumentNullException("siteMapLoader"); @@ -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; /// /// Maximal number of links per sitemap file. @@ -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( @@ -204,22 +208,21 @@ public override void ExecuteResult(ControllerContext context) // Sitemap file for all links ExecuteSitemapResult(context, flattenedHierarchy, flattenedHierarchyCount, 1); } - } - - /// - /// Generates the sitemap index elements. - /// - /// The number of pages. - /// The URL. - /// The site map URL template. - /// The sitemap index elements. - protected virtual IEnumerable GenerateSiteMapIndexElements(int numPages, string url, string siteMapUrlTemplate) + } + + /// + /// Generates the sitemap index elements. + /// + /// The number of pages. + /// The site map URL template. + /// The sitemap index elements. + protected virtual IEnumerable 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)); } } diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/XmlSiteMapResultFactory.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/XmlSiteMapResultFactory.cs index b9064dda..67093d1d 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/XmlSiteMapResultFactory.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/XmlSiteMapResultFactory.cs @@ -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) @@ -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 siteMapCacheKeys) @@ -61,8 +63,9 @@ public virtual ActionResult Create(IEnumerable siteMapCacheKeys) this.DefaultRootNode, siteMapCacheKeys, this.DefaultBaseUrl, - this.DefaultSiteMapUrlTemplate, - this.siteMapLoader); + this.DefaultSiteMapUrlTemplate, + this.siteMapLoader, + this.urlPath); } public ActionResult Create(int page, IEnumerable siteMapCacheKeys) @@ -72,8 +75,9 @@ public ActionResult Create(int page, IEnumerable siteMapCacheKeys) this.DefaultRootNode, siteMapCacheKeys, this.DefaultBaseUrl, - this.DefaultSiteMapUrlTemplate, - this.siteMapLoader); + this.DefaultSiteMapUrlTemplate, + this.siteMapLoader, + this.urlPath); } public virtual ActionResult Create(IEnumerable siteMapCacheKeys, string baseUrl, string siteMapUrlTemplate) @@ -83,8 +87,9 @@ public virtual ActionResult Create(IEnumerable siteMapCacheKeys, string this.DefaultRootNode, siteMapCacheKeys, baseUrl, - siteMapUrlTemplate, - this.siteMapLoader); + siteMapUrlTemplate, + this.siteMapLoader, + this.urlPath); } public ActionResult Create(int page, IEnumerable siteMapCacheKeys, string baseUrl, string siteMapUrlTemplate) @@ -94,8 +99,9 @@ public ActionResult Create(int page, IEnumerable siteMapCacheKeys, strin this.DefaultRootNode, siteMapCacheKeys, baseUrl, - siteMapUrlTemplate, - this.siteMapLoader); + siteMapUrlTemplate, + this.siteMapLoader, + this.urlPath); } public virtual ActionResult Create(ISiteMapNode rootNode) @@ -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) @@ -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) @@ -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) @@ -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