ASP.NET Futures (July 2007): Enabling Search Sitemaps with ASP.NET AJAX
This section provides information about the following:
- Sitemaps. Publishing an ASP.NET sitemap that is configured to assist search
engines.
- Dynamic data search sitemaps. Adding dynamic data from your application to a search
sitemap, such as information from a database.
Enabling Search Sitemaps
The ASP.NET Futures (May 2007) introduced a handler named SearchSiteMaps.axd
that dynamically generates sitemaps and a sitemap index that can be consumed by
search engines. Using this handler helps you specify what pages a search engine
indexes (crawls) and provides information to the search engine about the structure
of your Web site. For details about the protocol for search sitemaps, see the http://sitemaps.org/ Web site.
To create a search sitemap, create a section in the Web.config file named searchSiteMap.
Set the enabled attribute to true, and then register at least
one provider. The Futures release includes two providers:
- The
AspNetSiteMapSearchSiteMapProvider class generates a search sitemap from
an existing ASP.NET sitemap.
- The
DynamicDataSearchSiteMapProvider class generates a search sitemap from
a collection returned by application code.
You can also create a custom search sitemap provider.
The following example shows an example of the searchSiteMap section
of the Web.config file.
<microsoft.web.preview>
<searchSiteMap enabled="true">
<providers>
<add name="Navigation"
type="Microsoft.Web.Preview.Search.AspNetSiteMapSearchSiteMapProvider, Microsoft.Web.Preview"/>
</providers>
</searchSiteMap>
</microsoft.web.preview>
<system.web>
<httpHandlers>
...
<add verb="*" path="SearchSiteMaps.axd"
type="Microsoft.Web.Preview.Search.SearchSiteMapHandler" validate="True"/>
...
</httpHandlers>
</system.web>
..
Web.config
The following example shows the code.
Enabling search sitemaps
Dynamic Data Search Sitemap
You can add dynamic data (for example, information from a product catalog) as a
list of items to a search sitemap. Any information that is in a list that implements
IEnumerable can be added to the search sitemap.
To use dynamic data for a search sitemap, you add a class that derives from DynamicDataSearchSiteMapProvider
to the App_Code directory. You then add the class as an individual entry in the
searchSiteMap provider list. The search sitemap handler and the
DynamicDataSearchSiteMapProvider base class work together to call the
DataQuery method in your class.
The following properties are used to customize the output of the generated sitemap
entries:
- The
targetUrl property (required) specifies the URL of the page in
the sitemap.
- The
targetUrlseparator property (optional) specifies the seperator
between the URL and the data fields. The default is ?. You can specifiy
characters such as # or /.
- The
queryStringDataFormatString property (optional) specifies how the
data fields are formatted using syntax like that used by the String.Format method.
If no format is specified, the provider auto-generates a default format string.
- The
queryStringDataFields property (optional) specifies which columns
you want to bind in targetUrlFormatString. If the property is not specified,
the provider infers the list of column names from the collection returned by the
DataQuery method.
- The
lastModifiedDataField property (optional) specifies the column
that contains information about the last time the sitemap was modified. This date
should be in W3C datetime format, which allows you to omit the time portion and
provide the date in the format YYYY-MM-DD. If the property is not specified, the provider
attempts to read a property named SiteMapLastModified.
- The
changeFrequencyDataField property (optional) specifies how frequently
the page is likely to change. Valid values are: always, hourly, daily, weekly, monthly,
yearly, never. If the property is not specified, the provider attempts to read a property named SiteMapChangeFrequency.
- The
priorityDataField property (optional) specifies the priority of
this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0.
If the property is not specified, the provider attempts to read a property named SiteMapPriority.
- Set the
pathInfoFormat property (optional) to true if
you want to use only the value in the URL (for example, http://site/page.aspx/1)
The following example shows the Web.config file for using the dynamic data search sitemap.
<microsoft.web.preview>
<searchSiteMap enabled="true">
<providers>
<add name="Navigation"
type="Microsoft.Web.Preview.Search.AspNetSiteMapSearchSiteMapProvider, Microsoft.Web.Preview"/>
<add name="Supplier"
type="SupplierSiteMapData, App_Code"
targetUrl="Supplier.aspx"
/>
<add name="Supplier2"
type="SupplierSiteMapData, App_Code"
targetUrl="Supplier.aspx"
targetUrlseparator="#"
/>
<add name="Category"
type="CategorySiteMapData, App_Code"
targetUrl="Category_ajax.aspx"
queryStringDataFields="CategoryID,CategoryName"
/>
<add name="Category2"
type="CategorySiteMapData, App_Code"
targetUrl="Category_ajax.aspx"
queryStringDataFormatString="CategoryID={0}&CategoryName={1}"
queryStringDataFields="CategoryID,CategoryName"
/>
<add name="Category3"
type="CategorySiteMapData, App_Code"
targetUrl="Category_ajax.aspx"
lastModifiedDataField="lastmod"
changeFrequencyDataField="frequency"
priorityDataField="pri"
/>
<add name="Category4"
type="CategorySiteMapData, App_Code"
targetUrl="Category_ajax.aspx"
targetUrlseparator="#"
queryStringDataFormatString="CategoryID={0}&CategoryName={1}"
queryStringDataFields="CategoryID,CategoryName"
lastModifiedDataField="lastmod"
changeFrequencyDataField="frequency"
priorityDataField="pri"
/>
<add name="Product"
type="ProductSiteMapData, App_Code"
targetUrl="Product.aspx"
pathInfoFormat="true"
queryStringDataFields="ProductID"
/>
</providers>
</searchSiteMap>
</microsoft.web.preview>
<system.web>
<httpHandlers>
...
<add verb="*" path="SearchSiteMaps.axd" type="Microsoft.Web.Preview.Search.SearchSiteMapHandler" validate="True"/>
...
</httpHandlers>
</system.web>
Web.config
The following example shows code that returns sitemap data. The class derives from DynamicDataSearchSiteMapProvider.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.DLinq;
using System.IO;
using System.Text;
using System.Web;
using System.Query;
using System.Xml;
using Microsoft.Web.Preview.Search;
public class SupplierEntry
{
public String SupplierID;
public String SupplierName;
public String SiteMapLastModified;
public String SiteMapChangeFrequency;
public String SiteMapPriority;
}
public class SupplierSiteMapData : DynamicDataSearchSiteMapProvider
{
public override IEnumerable DataQuery()
{
List list = new List();
SupplierEntry s1 = new SupplierEntry();
s1.SupplierID = "3";
s1.SupplierName = "Grandma Kelly's Homestead";
s1.SiteMapLastModified = DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss.fffZ");
s1.SiteMapChangeFrequency ="";
s1.SiteMapPriority ="0.4";
list.Add(s1);
SupplierEntry s2 = new SupplierEntry();
s2.SupplierID = "8";
s2.SupplierName = "Specialty Biscuits, Ltd.";
s2.SiteMapLastModified = "";
s2.SiteMapChangeFrequency ="monthly";
s2.SiteMapPriority ="0.7";
list.Add(s2);
return list.ToArray();
}
}
Supplier code in App_Code
Dynamic Data Search Sitemap Example
The following example shows a complete example of how to implement a dynamic data search sitemap.
Adding dynamic data to search sitemaps
|