Đang nạp dữ liệu ...
CMS - MENU
HỌC TẬP

Sửa thông tin bài viết Generate Sitemaps for Google, MSN/Live, Yahoo, Ask on the fly using an ASP.NET HttpHandler

30 Tháng Mười Hai 2010 - Theo Internet
Lượt xem: 1.832     Phản hồi: 0

Overview

 

Here is a brief overview of how SiteMap HttpHandler will work: A request for SiteMap, will be intercepted and passed to our SiteMap HttpHandler which will generate the SiteMap XML.

Step 1: Create HttpHandler

 

Inside the App_Code folder, create SiteMapHandler.cs.








Type Code:
void IHttpHandler.ProcessRequest(HttpContext context)
{
    //
    // Important to return qualified XML (text/xml) for sitemaps
    //
    context.Response.ClearHeaders();
    context.Response.ClearContent();
    context.Response.ContentType = "text/xml";
    //
    // Create an XML writer
    //
    XmlTextWriter writer = new XmlTextWriter(context.Response.Output);
    writer.WriteStartDocument();
    writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
    //
    // Now add entries for individual pages..
    //
    writer.WriteStartElement("url");
    writer.WriteElementString("loc", "http://www.codingthewheel.com");
    // use W3 date format..
    writer.WriteElementString("lastmod", postDate.ToString("yyyy-MM-dd"));
    writer.WriteElementString("changefreq", "daily");
    writer.WriteElementString("priority", "1.0");
    writer.WriteEndElement();
    //
    // Close everything out and go home.
    //
    result.WriteEndElement();
    result.WriteEndDocument();
    writer.Flush();
}

I commented out the loop that adds pages. You may get URL of your pages from web.sitemap file, database or another sitemap provider.

Step 2: Modify Sitemap


Add the following section inside system.web



<httpHandlers>


   <add verb="*" path="sitemap.axd"


        type="SitemapHandler" validate="false"/>


</httpHandlers>

In order to test your sitemap; browse the sitemap.axd file.

Step 3: Use robots.txt to announce Sitemap to Search Engines

 

Create a text file in the root of your application and name it: robots.txt

Insert the following line changing bloggingdeveloper.com with your domain name:

Sitemap: http://www.bloggingdeveloper.com/sitemap.axd


BÌNH LUẬN:
Copyright © by ASP.NET VIETNAM
Contact us: ngothanhtung.it@gmail.com