Google SiteMap File in ASP.Net C#

Google provides a free service, named Google SiteMaps, that you can use to monitor and improve the way that Google indexes the pages on your website. For example, you can use Google SiteMaps to discover which Google search queries have returned pages from your website and the ranking of your pages in Google search results. You also can use Google SiteMaps to view any problems that the Google crawler encounters when indexing your site.

You can sign up for Google SiteMaps by visiting the following URL:
http://www.google.com/webmasters/sitemaps

To use Google SiteMaps, you must provide Google with the URL of a Google SiteMap file hosted on your website. The Google SiteMap file is an XML file that contains a list of URLs you want Google to index.

The Google SiteMap XML file has the following format:
<urlset xmlns=”http://www.google.com/schemas/sitemap/0.84”>
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005-01-01</lastmod>
</url>
<url>
<loc>http://www.example.com/sample.html/</loc>
<lastmod>2006-03-11</lastmod>
</url>
</urlset>

The Google SiteMap file contains a simple list of <url> elements that contain <loc> elements representing the location of the URL and <lastmod> elements representing the last modified date of the URL.

The Google SiteMap file also can contain <changefreq> and <priority> elements.The <changefreq> element indicates how frequently a URL changes, and the <priority> element represents the priority of a URL relative to other URLs in your site. These elements are optional and are ignored here.

<%@ WebHandler Language=”C#” Class=”PublicSiteMap” %>
using System;
using System.Web;
using System.Xml;
using System.Text;
using System.IO;
public class PublicSiteMap : IHttpHandler {
private XmlWriter _xmlWriter;
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = “text/xml”;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
_xmlWriter = XmlWriter.Create(context.Response.OutputStream,settings);
_xmlWriter.WriteStartDocument();
_xmlWriter.WriteStartElement(“urlset”,”http://www.google.com/schemas/sitemap/0.84”);
// Add root node
AddUrl(SiteMap.RootNode);
// Add all other nodes
SiteMapNodeCollection nodes = SiteMap.RootNode.GetAllNodes();
foreach (SiteMapNode node in nodes)
AddUrl(node);
_xmlWriter.WriteEndElement();
_xmlWriter.WriteEndDocument();
_xmlWriter.Flush();
}
private void AddUrl(SiteMapNode node)
{
// Skip empty Urls
if (String.IsNullOrEmpty(node.Url))
return;
// Skip remote nodes
if (node.Url.StartsWith(“http”, true, null))
return;
// Open url tag
_xmlWriter.WriteStartElement(“url”);
// Write location
_xmlWriter.WriteStartElement(“loc”);
_xmlWriter.WriteString(GetFullUrl(node.Url));
_xmlWriter.WriteEndElement();
// Write last modified
_xmlWriter.WriteStartElement(“lastmod”);
_xmlWriter.WriteString(GetLastModified(node.Url));
_xmlWriter.WriteEndElement();
// Close url tag
_xmlWriter.WriteEndElement();
}
private string GetFullUrl(string url)
{
HttpContext context = HttpContext.Current;
string server = context.Request.Url.GetComponents(UriComponents.SchemeAndServer,UriFormat.UriEscaped);
return Combine(server, url);
}
private string Combine(string baseUrl, string url)
{
baseUrl = baseUrl.TrimEnd(new char[] {‘/’});
url = url.TrimStart(new char[] { ‘/’ });
return baseUrl + “/” + url;
}
private string GetLastModified(string url)
{
HttpContext context = HttpContext.Current;
string physicalPath = context.Server.MapPath(url);
return File.GetLastWriteTimeUtc(physicalPath).ToString(“s”) + “Z”;
}
public bool IsReusable {
get {
return true;
}
}
}

You can think of an HTTP Handler is a lightweight ASP.NET page. You learn about HTTP Handlers in Chapter 27, “Working with the HTTP Runtime.”

<urlset xmlns=”http://www.google.com/schemas/sitemap/0.84”>
<url>
<loc>http://localhost:1104/SiteMaps/Default.aspx</loc>
<lastmod>2005-10-30T03:13:58Z</lastmod>
</url>
<url>
<loc>http://localhost:1104/SiteMaps/Products/Default.aspx</loc>
<lastmod>2005-10-28T21:48:04Z</lastmod>
</url>
<url>
<loc>http://localhost:1104/SiteMaps/Services</loc>
<lastmod>2005-10-30T04:31:57Z</lastmod>
</url>
<url>
<loc>http://localhost:1104/SiteMaps/Employees/Default.aspx</loc>
<lastmod>1601-01-01T00:00:00Z</lastmod>
</url>
<url>
<loc>http://localhost:1104/SiteMaps/Products/FirstProduct.aspx</loc>
<lastmod>2005-10-30T03:43:52Z</lastmod>
</url>
</urlset>

When you sign up at the Google SiteMaps website, submit the URL of the PublicSiteMap.ashx file when you are asked to enter your SiteMap URL. The Google service retrieves your SiteMap from the handler automatically.
Tags: , , ,
Hot on Web:


About author