Explanation:
We will directly go to the usefull code and create a Class call "GoogleSiteMapItem".
It consist of Five (5) Properties and Two (2) Class Constructor. This class is a
simple business object that contains row data for the googlesitemap.
Create a new Web application or in your Web Application select the "App-Code"
Folder and Create a New Class. Name this Class "GoogleSitemapItem".
GoogleSiteMapItem Class;
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class GoogleSitemapItem
{
public GoogleSitemapItem()
{ }
public GoogleSitemapItem(string loc,
string lastMod, string changeFreq, string priority)
{
Loc = loc;
LastMod = lastMod;
ChangeFreq =
changeFreq;
Priority = priority;
}
private string _loc;
public string Loc
{
get { return
_loc; }
set { _loc =
value; }
}
private string _lastMod;
public string LastMod
{
get { return
_lastMod; }
set { _lastMod
= value; }
}
private string _changeFreq;
public string ChangeFreq
{
get { return
_changeFreq; }
set { _changeFreq
= value; }
}
private string _priority;
public string Priority
{
get { return
_priority; }
set { _priority
= value; }
}
}
In your Main application Create a New Page "GoogleSitemap.aspx". Select
also the "Code Behind" Check box from the File Creation Dialoge.
GoogleSiteMap Web Form;
Copy the Following Code to your Page.
<%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeFile="GoogleSitemap.aspx.cs"
Inherits="GoogleSitemap" %>
<asp:repeater id="SiteMapRpt" runat="server">
<headertemplate>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
</headertemplate>
<itemtemplate>
<url>
<loc><%#Eval("Loc")%></loc>
<lastmod><%#Eval("LastMod")%></lastmod>
<changefreq><%#Eval("ChangeFreq")%></changefreq>
<priority><%#Eval("Priority")%></priority>
</url>
</itemtemplate>
<footertemplate>
</urlset>
</footertemplate>
</asp:repeater>
GoogleSiteMap Code Behind;
This is where we get to define our site pages, and where the GoogleSiteMapItem objects
you created come into play.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevDistrict.Data;
using DevDistrict.BusinessObjects;
public partial class GoogleSitemap : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
List<GoogleSitemapItem> urls =
new List<GoogleSitemapItem>();
//Dotnet-Friends
urls.Add(new GoogleSitemapItem("http://dotnet-friends.com/",DateTime.Today.ToString("yyyy-MM-dd"),"daily","1.0"));
urls.Add(new GoogleSitemapItem("http://dotnet-friends.com/Code.aspx",
DateTime.Today.ToString("yyyy-MM-dd"), "daily", "0.9"));
urls.Add(new GoogleSitemapItem("http://dotnet-friends.com/Download.aspx",
DateTime.Today.ToString("yyyy-MM-dd"), "daily", "0.8"));
//Specific to DevDistrict. This grabs
all the articles (code snippets) on DevDistrict and
//creates a GoogleSitemapItem for each
of them, since they are all dynamic pages this ensures they get
//get some Google facetime.
List<Article> articles = DataController.GetAllArticles();
foreach(Article a in articles)
{
urls.Add(new
GoogleSitemapItem("http://dotnet-friends.com/CodeDetails.aspx?A=" + a.ArticleId.ToString(),
DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.8"));
}
urls.Add(new GoogleSitemapItem("http://dotnet-friends.com/Links.aspx",
DateTime.Today.ToString("yyyy-MM-dd"), "daily", "0.6"));
urls.Add(new GoogleSitemapItem("http://dotnet-friends.com/About.aspx",
DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));
urls.Add(new GoogleSitemapItem("http://dotnet-friends.com/Contact.aspx",
DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));
urls.Add(new GoogleSitemapItem("http://dotnet-friends.com/Disclaimer.aspx",
DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));
urls.Add(new GoogleSitemapItem("http://dotnet-friends.com/Privacy.aspx",
DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));
urls.Add(new GoogleSitemapItem("http://dotnet-friends.com/Search.aspx",
DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));
SiteMapRpt.DataSource = urls;
SiteMapRpt.DataBind();
}
}
This code creates a List of GoogleSiteMapItem objects. Each page of my site is then
added to the list as a GoogleSiteMapItem. At the end the List is bound to the DataRepeater
control on our page and presto we are done!
Note that this code always tells google that the page was updated at the current
date/time. This is just for your reference, feel free to pull these values out of
a database or some other data store.
|