Kalyani Shevale

Kalyani Shevale

  • 606
  • 3.2k
  • 158.5k

how to add sitemap.xml file in mvc

Mar 12 2020 5:45 AM
I have added sitemap file in my projects.
 below is sitemap controller.
 
  1. public class SitemapController : Controller  
  2.     {  
  3.         // GET: Sitemap  
  4.         public ActionResult Index()  
  5.         {  
  6.             StringBuilder sitemap = new StringBuilder();  
  7.   
  8.             sitemap.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");  
  9.             sitemap.AppendLine("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">");  
  10.   
  11.             BuildSitemap(new CmsPages().GetAllPages(), new Languages().GetAllLanguages(isActive: true), new GlobalConfigurations().GetGlobalConfiguration(), nullref sitemap);  
  12.   
  13.             sitemap.AppendLine("</urlset>");  
  14.   
  15.             return Content(sitemap.ToString(), "text/xml");  
  16.         }  
  17.   
  18.         private static void BuildSitemap(List<CmsPage> cmsPageList, List<Language> sitemapLanguages, GlobalConfiguration globalConfiguration, int? pageParentId, ref StringBuilder sitemap)  
  19.         {  
  20.             if (cmsPageList.IsNotNull())  
  21.             {  
  22.                 List<CmsPage> sitemapPages = (from page in cmsPageList  
  23.                                               where page.PageParentId == pageParentId  
  24.                                               && page.ShowInSitemap  
  25.                                               && page.IsActive  
  26.                                               select page).ToList();  
  27.   
  28.                 if (sitemapPages.IsNotNull() && sitemapPages.Count() > 0)  
  29.                 {  
  30.                     foreach (CmsPage page in sitemapPages)  
  31.                     {  
  32.                         foreach (Language language in sitemapLanguages)  
  33.                         {  
  34.                             sitemap.AppendLine("  <url>");  
  35.                             sitemap.AppendLine("    <loc>" + globalConfiguration.DomainName.ToUrl() + (page.IsHomePage ? (language.LanguageCode != globalConfiguration.DefaultLanguageCode ? language.LanguageCode + "/" : "") : language.LanguageCode + "/" + page.FullSegment + "/") + "</loc>");  
  36.                             foreach (Language languageSubset in sitemapLanguages)  
  37.                             {  
  38.                                 sitemap.AppendLine("    <xhtml:link rel=\"alternate\" hreflang=\"" + languageSubset.LanguageCode + "\" href=\"" + globalConfiguration.DomainName.ToUrl() + (page.IsHomePage ? (languageSubset.LanguageCode != globalConfiguration.DefaultLanguageCode ? languageSubset.LanguageCode + "/" : "") : languageSubset.LanguageCode + "/" + page.FullSegment + "/") + "\" />");  
  39.                             }  
  40.                             sitemap.AppendLine("  </url>");  
  41.                         }  
  42.   
  43.                         BuildSitemap(cmsPageList, sitemapLanguages, globalConfiguration, page.PageId, ref sitemap);  
  44.                     }  
  45.                 }  
  46.             }  
  47.         }  
  48.     }  
 
route.config code for sitemap
  1. routes.MapRoute(  
  2.     name: "Sitemap",  
  3.     url: "sitemap.xml",  
  4.     defaults: new { controller = "Sitemap", action = "Index" }  
  5. );  
 
 web.config site map url
 
  1. <handlers>  
  2.      <add name="SitemapXmlHandler" path="sitemap.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" />  
  3.    </handlers>  
 please where is issue for sitemap.

Answers (1)