Create automated sitemap using

Task: Create automated sitemap dynamically using 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 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.Charset = "UTF-8";
        context.Response.Write("<?xml version='1.0' encoding='UTF-8'?><urlset xmlns=''>");

    public bool IsReusable {
        get {
            return false;
    public string sitemap()
        string weburl = "";
        System.Text.StringBuilder sitemap = new System.Text.StringBuilder();
        <loc>" + weburl + @"</loc>
        // Append more url in your sitemap here using database
        return sitemap.ToString();

No comments:

Post a Comment