📜 ⬆️ ⬇️

Generate Sitemaps on the fly using ASP.NET HttpHandler

Sitemaps is an XML file with information for search engines (such as Google, Yahoo, Ask.com, MSN, Yandex) about the pages of a website that are subject to indexing. Sitemaps can help search engines locate pages on a site, the time they were last updated, the frequency of updates, and importance relative to other pages on a site so that the search engine can more intelligently index a site.

Using the Sitemaps protocol does not guarantee that web pages will be indexed by search engines, this is just an additional hint for crawlers who can perform a more thorough scan of your site. The article describes how to get Sitemaps on the fly using ASP.NET HttpHandler.


An example of a sitemap that contains a single page:
')
<? xml version ="1.0" encoding ="UTF-8" ? >
< urlset xmlns ="http://www.sitemaps.org/schemas/sitemap/0.9" >
< url >
< loc > habrahabr.ru </ loc >
< lastmod > 2009-10-25 </ lastmod >
< changefreq > monthly </ changefreq >
< priority > 0.8 </ priority >
</ url >
</ urlset >


* This source code was highlighted with Source Code Highlighter .


You can read more about the protocol here .

Inside the App_Code directory, create SiteMapHandler.cs:

image

image

Below is the Asp.Net Sitemap Handler code that implements the IHttpHandler interface:

public class SitemapHandler : IHttpHandler
{
protected enum ChangeFrequency
{
always,
hourly,
daily,
weekly,
monthly,
yearly,
never
}

#region IHttpHandler Members

public bool IsReusable
{
get { return false ; }
}

public void ProcessRequest( HttpContext context)
{
using (TextWriter textWriter = new StreamWriter(context.Response.OutputStream, System.Text. Encoding .UTF8))
{
XmlTextWriter writer = new XmlTextWriter(textWriter);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement( "urlset" );
writer.WriteAttributeString( "xmlns" , "http://www.sitemaps.org/schemas/sitemap/0.9" );

//Add home page
writer.WriteStartElement( "url" );
writer.WriteElementString( "loc" , "http://habrahabr.ru/" );
writer.WriteElementString( "lastmod" , DateTime .Now.ToString( "yyy-MM-dd" , System.Globalization.CultureInfo.InvariantCulture));
writer.WriteElementString( "changefreq" , ChangeFrequency.always.ToString());
writer.WriteElementString( "priority" , "0.8" );
writer.WriteEndElement(); // url

// Your code here for page nodes

writer.WriteEndElement(); // urlset
}
context.Response.ContentType = "text/xml" ;
}

#endregion
}


* This source code was highlighted with Source Code Highlighter .


In the place of the comment “Your code here for page nodes” you need to insert your own logic for adding pages — either from the database, or from a web.sitemap, or from another source.

Add the following lines to web.config:

< httpHandlers >
< add verb ="*" path ="sitemap.axd"
type ="SitemapHandler" validate ="false" />
</ httpHandlers >


* This source code was highlighted with Source Code Highlighter .


To test your Sitemap, open it in the browser line Sitemap.axd.

Next, you need to create a robots.txt file and make a line there:

Sitemap: youdomain / sitemap.axd

The article used materials from the bloggingdeveloper website.

Thanks for attention!

Source: https://habr.com/ru/post/73303/


All Articles