Task:
Create automated sitemap dynamically using asp.net generic web handler.
Description: In E-Commerce website or blog we need to update sitemap regularly, instead of generating sitemap using third-party tool, we can generate sitemap dynamically using asp.net. Using "ashx" generic handler we can generate xml sitemap & using url rewriting we can rewrite "sitemap.ashx" for "sitemap.xml" request.
Description: In E-Commerce website or blog we need to update sitemap regularly, instead of generating sitemap using third-party tool, we can generate sitemap dynamically using asp.net. Using "ashx" generic handler we can generate xml sitemap & using url rewriting we can rewrite "sitemap.ashx" for "sitemap.xml" request.
<%@ WebHandler Language="C#"
Class="sitemap"
%>
using System;
using
System.Web;
using
System.Text;
using
System.Data;
public class sitemap : IHttpHandler {
string
currentTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");
public void ProcessRequest (HttpContext
context) {
context.Response.ContentType = "text/xml";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetAllowResponseInBrowserHistory(true);
context.Response.Charset = "UTF-8";
context.Response.Write("<?xml version='1.0'
encoding='UTF-8'?><urlset
xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>");
context.Response.Write(sitemap());
context.Response.Write("</urlset>");
}
public bool IsReusable {
get {
return
false;
}
}
public string sitemap()
{
string
weburl = "http://hemantrautela.blogspot.com/";
System.Text.StringBuilder
sitemap = new System.Text.StringBuilder();
sitemap.Append(@"<url>
<loc>" + weburl +
@"</loc>
<lastmod>"+currentTime+@"</lastmod>
<priority>1.00</priority>
</url>");
// Append more url in your sitemap here using database
return
sitemap.ToString();
}
No comments:
Post a Comment