Related Tags:
Asp.Net It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology. Learn More, C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, Sitemap A sitemap is a file where you provide information about the pages, videos, and other files on your site, and the relationships between them. Search engines like Google read this file to more intelligently crawl your site. Learn More,

How to dynamically generate sitemap xml for an asp.net mvc website?

Description:I have a website developed using asp.net mvc (c#) which contains some static pages and also some dynamic pages i.e where the content is fetched from database and is dynamically increasing as well, So I need to build such a sitemap for which I need to code for once and it take care of the future items that needs to be included in the sitemap.

Posted by: | Posted on: Jan 21, 2020

1 answers

Replies

6

The simplest way I found on web to generate such dynamic sitemap using mvc asp.net(c#) is following a three steps procedure in which we just need to create two classes and a controller method.
Step 1 – Create A CustomSitemapItem Class
The CustomSitemapItem Class will be used to store all the properties we need for each sitemap URL entry.

public class CustomSitemapItem {
public DateTime? DateAdded { get; set; }
public string URL { get; set; }
public string Priority { get; set; }
}

Step 2 – Create A CustomSitemapActionResult

public class CustomSitemapActionResult : ActionResult
{
private List<CustomSitemapItem> _SitemapItems;
private string _Website;
public CustomSitemapActionResult(List<CustomSitemapItem> SitemapItems, string Website)
{
this._SitemapItems = SitemapItems;
this._Website = Website;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "text/xml";
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
{
writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
foreach (var SiteMapItem in this._SitemapItems)
{
writer.WriteStartElement("url");
writer.WriteElementString("loc", string.Format(this._Website + "{0}", SiteMapItem.URL));
if (SiteMapItem.DateAdded != null)
{
writer.WriteElementString("lastmod", string.Format("{0:yyyy-MM-dd}", SiteMapItem.DateAdded));
}
writer.WriteElementString("changefreq", "daily");
writer.WriteElementString("priority", SiteMapItem.Priority);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
}
}

The CustomSitemapActionResult is the result that comes back from the controller when the URL /sitemap is called. It takes a list of CustomSitemapItem classes and generates the corresponding xml file that will be used by different search engines to index your website.
Step 3 – Create A Sitemap Controller Method
The controller is where you will generate your list of CustomSitemapItem classes that will be returned via the CustomSitemapActionResult

[OutputCache(Duration = 120, VaryByParam = "none")]
public CustomSitemapActionResult Sitemap()
{
var Website = "http://www.mysitedomain.com";
var currentdate = DateTime.Now;
var SitemapItems = new List<CustomSitemapItem>();
//Static items will be added as follows
SitemapItems.Add(new CustomSitemapItem
{
URL = "",
Priority = "1",
DateAdded = currentdate
});
SitemapItems.Add(new CustomSitemapItem
{
URL = "/about",
Priority = ".9",
DateAdded = currentdate
});
SitemapItems.Add(new CustomSitemapItem
{
URL = "/contact",
Priority = ".9",
DateAdded = currentdate
});
SitemapItems.Add(new CustomSitemapItem
{
URL = "/terms-and-condition",
Priority = ".9",
DateAdded = currentdate
});

//Dynamic items will be added as follows
var allNews = News.GetWithAllStatus().Where(t => t.Status == Status.Active);
foreach (var item in allNews)
{
int totalCount = 0;
SitemapItems.Add(new CustomSitemapItem
{
URL = "/News/" + item.UrlName,
Priority = ".8",
DateAdded = item.DateCreated
});
}
//Dynamic items will be added as follows
var allBlogPosts = BlogPosts.GetWithAllStatus().Where(p => p.Status == Status.Active);
foreach (var item in allBlogPosts)
{
SitemapItems.Add(new CustomSitemapItem
{
URL = "/Blogpost/" + item.UrlName,
Priority = ".8",
DateAdded =item.DateCreated
});
}
return new CustomSitemapActionResult(SitemapItems, Website);
}

That’s it now you can simply use the controller method Sitemap() to get your sitemap xml.

Replied by: | Replied on: Mar 09, 2020



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview