ASP.NET Tutorial/Development/Site Maps
Содержание
- 1 Adding nodes to a Site Map dynamically.
- 2 Creating a custom navigation display using the CurrentNode property (C#)
- 3 Creating a custom navigation display using the CurrentNode property (VB)
- 4 Creating Custom Site Map Attributes
- 5 Creating the AutoSiteMapProvider
- 6 Defining a Site Map
- 7 Displaying an automatically generated Site Map.
- 8 Format SiteMapPath
- 9 Merging Multiple Site Maps
- 10 Removing the root node from the retrieved node collection
- 11 The SiteMapPath control enables you to navigate to any parent page of the current page.
- 12 To display different links to different users depending on their roles, enable the Security Trimming
- 13 Using a template with the SiteMapPath control.
- 14 Using the SiteMap Class
- 15 Using the SiteMapNode Class
- 16 using the StartFromCurrentNode property
- 17 Using the StartingNodeUrl property
- 18 Working with the CurrentNode object (C#)
- 19 Working with the CurrentNode object (VB)
Adding nodes to a Site Map dynamically.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>About</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SiteMapPath
id="SiteMapPath1"
Runat="server" />
<hr />
<h1>About Our Company</h1>
</div>
</form>
</body>
</html>
File: Web.sitemap
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode
url="Default.aspx"
title="Home"
description="The Home Page">
<siteMapNode
url="Default1.aspx"
title="Our Products"
description="Products that we offer">
<siteMapNode
url="FirstProduct.aspx"
title="First Product"
description="The description of the First Product" />
<siteMapNode
url="SecondProduct.aspx"
title="Second Product"
description="The description of the Second Product" />
</siteMapNode>
<siteMapNode
url="Default2.aspx"
title="Our Services"
description="Services that we offer">
<siteMapNode
url="FirstService.aspx"
title="First Service"
description="The description of the First Service"
metaDescription="The first service" />
<siteMapNode
url="SecondService.aspx"
title="Second Service"
description="The description of the Second Service" />
</siteMapNode>
</siteMapNode>
</siteMap>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
Hyperlink1.Text = SiteMap.CurrentNode.ParentNode.ToString();
Hyperlink1.NavigateUrl = SiteMap.CurrentNode.ParentNode.Url;
Hyperlink2.Text = SiteMap.CurrentNode.PreviousSibling.ToString();
Hyperlink2.NavigateUrl = SiteMap.CurrentNode.PreviousSibling.Url;
Hyperlink3.Text = SiteMap.CurrentNode.NextSibling.ToString();
Hyperlink3.NavigateUrl = SiteMap.CurrentNode.NextSibling.Url;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>SiteMapDataSource</title>
</head>
<body>
<form id="form1" runat="server">
Move Up:
<asp:Hyperlink ID="Hyperlink1" Runat="server"></asp:Hyperlink><br />
<asp:Hyperlink ID="Hyperlink2" Runat="server"></asp:Hyperlink> |
<asp:Hyperlink ID="Hyperlink3" Runat="server"></asp:Hyperlink>
</form>
</body>
</html>
File: Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx">
<siteMapNode title="News" description="The Latest News" url="News.aspx">
<siteMapNode title="U.S." description="U.S. News" url="News.aspx?cat=us" />
<siteMapNode title="World" description="World News" url="News.aspx?cat=world" />
<siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" />
<siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" />
</siteMapNode>
<siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" />
</siteMapNode>
</siteMap>
<%@ Page Language="VB" %>
<script runat="server" language="vb">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Hyperlink1.Text = SiteMap.CurrentNode.ParentNode.ToString()
Hyperlink1.NavigateUrl = SiteMap.CurrentNode.ParentNode.Url
Hyperlink2.Text = SiteMap.CurrentNode.PreviousSibling.ToString()
Hyperlink2.NavigateUrl = SiteMap.CurrentNode.PreviousSibling.Url
Hyperlink3.Text = SiteMap.CurrentNode.NextSibling.ToString()
Hyperlink3.NavigateUrl = SiteMap.CurrentNode.NextSibling.Url
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>SiteMapDataSource</title>
</head>
<body>
<form id="form1" runat="server">
Move Up:
<asp:Hyperlink ID="Hyperlink1" Runat="server"></asp:Hyperlink><br />
<asp:Hyperlink ID="Hyperlink2" Runat="server"></asp:Hyperlink> |
<asp:Hyperlink ID="Hyperlink3" Runat="server"></asp:Hyperlink>
</form>
</body>
</html>
File: Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx">
<siteMapNode title="News" description="The Latest News" url="News.aspx">
<siteMapNode title="U.S." description="U.S. News" url="News.aspx?cat=us" />
<siteMapNode title="World" description="World News" url="News.aspx?cat=world" />
<siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" />
<siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" />
</siteMapNode>
<siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" />
</siteMapNode>
</siteMap>
Creating Custom Site Map Attributes
Any custom attributes that you add to a Site Map are exposed by instances of the SiteMapNode class.
File: Web.sitemap
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode
url="Default.aspx"
title="Home"
description="The Home Page">
<siteMapNode
url="Default1.aspx"
title="Our Products"
description="Products that we offer">
<siteMapNode
url="FirstProduct.aspx"
title="First Product"
description="The description of the First Product" />
<siteMapNode
url="SecondProduct.aspx"
title="Second Product"
description="The description of the Second Product" />
</siteMapNode>
<siteMapNode
url="Default2.aspx"
title="Our Services"
description="Services that we offer">
<siteMapNode
url="FirstService.aspx"
title="First Service"
description="The description of the First Service"
metaDescription="The first service" />
<siteMapNode
url="SecondService.aspx"
title="Second Service"
description="The description of the Second Service" />
</siteMapNode>
</siteMapNode>
</siteMap>
File: FirstService.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
void Page_Load()
{
HtmlMeta meta = new HtmlMeta();
meta.Name = "Description";
meta.Content = SiteMap.CurrentNode["metaDescription"];
head1.Controls.Add(meta);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
<title>First Service</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>The First Service</h1>
</div>
</form>
</body>
</html>
Creating the AutoSiteMapProvider
File: App_Code/AutoSiteMapProvider.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Caching;
namespace MyNamespace
{
public class AutoSiteMapProvider : StaticSiteMapProvider
{
private SiteMapNode _rootNode;
private static List<string> _excluded = new List<string>();
private List<string> _dependencies = new List<string>();
static AutoSiteMapProvider()
{
_excluded.Add("app_code");
_excluded.Add("app_data");
_excluded.Add("app_themes");
_excluded.Add("bin");
}
protected override SiteMapNode GetRootNodeCore()
{
return BuildSiteMap();
}
public override SiteMapNode BuildSiteMap()
{
lock (this)
{
HttpContext context = HttpContext.Current;
_rootNode = (SiteMapNode)context.Cache["RootNode"];
if (_rootNode == null)
{
Clear();
string folderUrl = HttpRuntime.AppDomainAppVirtualPath;
string defaultUrl = folderUrl + "/Default.aspx";
_rootNode = new SiteMapNode(this, folderUrl, defaultUrl, "Home");
AddNode(_rootNode);
AddChildNodes(_rootNode);
_dependencies.Add(HttpRuntime.AppDomainAppPath);
CacheDependency fileDependency = new CacheDependency(_dependencies.ToArray());
context.Cache.Insert("RootNode", _rootNode, fileDependency);
}
return _rootNode;
}
}
private void AddChildNodes(SiteMapNode parentNode)
{
AddChildFolders(parentNode);
AddChildPages(parentNode);
}
private void AddChildFolders(SiteMapNode parentNode)
{
HttpContext context = HttpContext.Current;
string parentFolderPath = context.Server.MapPath(parentNode.Key);
DirectoryInfo folderInfo = new DirectoryInfo(parentFolderPath);
DirectoryInfo[] folders = folderInfo.GetDirectories();
foreach (DirectoryInfo folder in folders)
{
if (!_excluded.Contains(folder.Name.ToLower()))
{
string folderUrl = parentNode.Key + "/" + folder.Name;
SiteMapNode folderNode = new SiteMapNode(this, folderUrl, null, GetName(folder.Name));
AddNode(folderNode, parentNode);
AddChildNodes(folderNode);
_dependencies.Add(folder.FullName);
}
}
}
private void AddChildPages(SiteMapNode parentNode)
{
HttpContext context = HttpContext.Current;
string parentFolderPath = context.Server.MapPath(parentNode.Key);
DirectoryInfo folderInfo = new DirectoryInfo(parentFolderPath);
FileInfo[] pages = folderInfo.GetFiles("*.aspx");
foreach (FileInfo page in pages)
{
if (!_excluded.Contains(page.Name.ToLower()))
{
string pageUrl = parentNode.Key + "/" + page.Name;
if (String.rupare(pageUrl, _rootNode.Url, true) !=0)
{ SiteMapNode pageNode = new SiteMapNode(this, pageUrl, pageUrl, GetName(page.Name));
AddNode(pageNode, parentNode);
}
}
}
}
private string GetName(string name)
{
name = Path.GetFileNameWithoutExtension(name);
return name.Replace("_", " ");
}
}
}
File: Web.Config
<configuration xmlns="http://schemas.microsoft.ru/.NetConfiguration/v2.0">
<system.web>
<siteMap defaultProvider="MyAutoSiteMapProvider">
<providers>
<add
name="MyAutoSiteMapProvider"
type="MyNamespace.AutoSiteMapProvider" />
</providers>
</siteMap>
</system.web>
</configuration>
Defining a Site Map
The XmlSiteMapProvider looks for a file named Web.sitemap in the root of the virtual directory.
XmlSiteMapProvider extracts the site map data and create the corresponding SiteMap object.
This SiteMap object is then made available to the SiteMapDataSource, which you place on every page that uses navigation.
Rule 1: Site Maps Begin with the <siteMap> Element.
Every Web.sitemap file begins by declaring the <siteMap>.
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0">
...
</siteMap>
Rule 2: Each Page Is Represented by a <siteMapNode> Element
To insert a page into the site map, you add the <siteMapNode> element.
You need to supply the title of the page, a description, and the URL.
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0">
<siteMapNode title="Home" description="Home" url="~/default.aspx" />
</siteMap>
The ~/ characters represent the root folder of your web application.
Rule 3: A <siteMapNode> Element Can Contain Other <siteMapNode> Elements
<siteMapNode title="Home" description="Home" url="~/default.aspx">
<siteMapNode title="Products" description="Our products" url="~/products.aspx" />
<siteMapNode title="Hardware" description="Hardware choices" url="~/hardware.aspx" />
</siteMapNode>
You can omit the url attribute, as shown here with the Products node:
<siteMapNode title="Products" description="Products">
<siteMapNode title="In Stock" description="Products that are available" url="~/inStock.aspx" />
<siteMapNode title="Not In Stock" description="Products that are on order" url="~/outOfStock.aspx" />
</siteMapNode>
Rule 4: Every Site Map Begins with a Single <siteMapNode>
A site map must always have a single root node.
Rule 5: Duplicate URLs Are Not Allowed
Displaying an automatically generated Site Map.
File: App_Code\SqlSiteMapProvider.cs
using System;
using System.Collections.Specialized;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Caching;
namespace MyNamespace
{
public class SqlSiteMapProvider : StaticSiteMapProvider
{
private bool _isInitialized = false;
private string _connectionString;
private SiteMapNode _rootNode;
public override void Initialize(string name, NameValueCollection attributes)
{
if (_isInitialized) return;
base.Initialize(name, attributes);
string connectionStringName = attributes["connectionStringName"];
if (String.IsNullOrEmpty(connectionStringName))
throw new Exception("You must provide a connectionStringName attribute");
_connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
if (String.IsNullOrEmpty(_connectionString))
throw new Exception("Could not find connection string " + connectionStringName);
_isInitialized = true;
}
protected override SiteMapNode GetRootNodeCore()
{
return BuildSiteMap();
}
public override SiteMapNode BuildSiteMap()
{
lock (this)
{
HttpContext context = HttpContext.Current;
_rootNode = (SiteMapNode)context.Cache["RootNode"];
if (_rootNode == null)
{
HttpContext.Current.Trace.Warn("Loading from database");
Clear();
DataTable tblSiteMap = GetSiteMapFromDB();
_rootNode = GetRootNode(tblSiteMap);
AddNode(_rootNode);
BuildSiteMapRecurse(tblSiteMap, _rootNode);
SqlCacheDependency sqlDepend = new SqlCacheDependency("SiteMapDB", "SiteMap");
context.Cache.Insert("RootNode", _rootNode, sqlDepend);
}
return _rootNode;
}
}
private DataTable GetSiteMapFromDB()
{
string selectCommand = "SELECT Id,ParentId,Url,Title,Description FROM SiteMap";
SqlDataAdapter dad = new SqlDataAdapter(selectCommand, _connectionString);
DataTable tblSiteMap = new DataTable();
dad.Fill(tblSiteMap);
return tblSiteMap;
}
private SiteMapNode GetRootNode(DataTable siteMapTable)
{
DataRow[] results = siteMapTable.Select("ParentId IS NULL");
if (results.Length == 0)
throw new Exception("No root node in database");
DataRow rootRow = results[0];
return new SiteMapNode(this, rootRow["Id"].ToString(), rootRow["url"].ToString(), rootRow["title"].ToString(), rootRow["description"].ToString());
}
private void BuildSiteMapRecurse(DataTable siteMapTable, SiteMapNode parentNode)
{
DataRow[] results = siteMapTable.Select("ParentId=" + parentNode.Key);
foreach (DataRow row in results)
{
SiteMapNode node = new SiteMapNode(this, row["Id"].ToString(), row["url"].ToString(), row["title"].ToString(), row["description"].ToString());
AddNode(node, parentNode);
BuildSiteMapRecurse(siteMapTable, node);
}
}
}
}
File: Web.Config
<configuration>
<connectionStrings>
<add
name="conSiteMap"
connectionString="Data Source=.\SQLExpress;Integrated
Security=True;AttachDbFileName=|DataDirectory|SiteMapDB.mdf;User Instance=True"/>
</connectionStrings>
<system.web>
<siteMap defaultProvider="myProvider">
<providers>
<add
name="myProvider"
type="MyNamespace.SqlSiteMapProvider"
connectionStringName="conSiteMap" />
</providers>
</siteMap>
<caching>
<sqlCacheDependency enabled = "true" pollTime = "5000" >
<databases>
<add name="SiteMapDB"
connectionStringName="conSiteMap"
/>
</databases>
</sqlCacheDependency>
</caching>
</system.web>
</configuration>
Format SiteMapPath
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<style type="text/css">
.siteMapPath
{
font:20px Comic Sans MS,Serif;
}
.currentNodeStyle
{
font-weight:bold;
}
.nodeStyle
{
text-decoration:none;
}
.pathSeparatorStyle
{
background-color:yellow;
margin:10px;
border:Solid 1px black;
}
.rootNodeStyle
{
text-decoration:none;
}
</style>
<title>SiteMapPath Style</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SiteMapPath
id="SiteMapPath1"
CssClass="siteMapPath"
CurrentNodeStyle-CssClass="currentNodeStyle"
NodeStyle-CssClass="nodeStyle"
PathSeparatorStyle-CssClass="pathSeparatorStyle"
RootNodeStyle-CssClass="rootNodeStyle"
Runat="server" />
<hr />
<h1>SiteMapPath Style</h1>
</div>
</form>
</body>
</html>
File: Web.sitemap
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode
url="Default.aspx"
title="Home"
description="The Home Page">
<siteMapNode
url="Default1.aspx"
title="Our Products"
description="Products that we offer">
<siteMapNode
url="FirstProduct.aspx"
title="First Product"
description="The description of the First Product" />
<siteMapNode
url="SecondProduct.aspx"
title="Second Product"
description="The description of the Second Product" />
</siteMapNode>
<siteMapNode
url="Default2.aspx"
title="Our Services"
description="Services that we offer">
<siteMapNode
url="FirstService.aspx"
title="First Service"
description="The description of the First Service"
metaDescription="The first service" />
<siteMapNode
url="SecondService.aspx"
title="Second Service"
description="The description of the Second Service" />
</siteMapNode>
</siteMapNode>
</siteMap>
Merging Multiple Site Maps
File: Web.sitemap
Code View: Scroll / Show All
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode
url="Default.aspx"
title="Home"
description="The Home Page">
<siteMapNode
siteMapFile="Employees/Employees.sitemap" />
</siteMapNode>
</siteMap>
File: Employees/Employees.sitemap
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode
url="Employees/Default.aspx"
title="Employees"
description="Contains descriptions of employees">
<siteMapNode
url="Employees/BillGates.aspx"
title="Bill Gates"
description="Bill Gates Page" />
<siteMapNode
url="Employees/SteveJobs.aspx"
title="Steve Jobs"
description="Steve Jobs Page" />
</siteMapNode>
</siteMap>
Removing the root node from the retrieved node collection
<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Menu Server Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server"
ShowStartingNode="False" />
<asp:Menu ID="Menu1" Runat="server" DataSourceID="SiteMapDataSource1">
</asp:Menu>
</form>
</body>
</html>
File: Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx">
<siteMapNode title="News" description="The Latest News" url="News.aspx">
<siteMapNode title="U.S." description="U.S. News" url="News.aspx?cat=us" />
<siteMapNode title="World" description="World News" url="News.aspx?cat=world" />
<siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" />
<siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" />
</siteMapNode>
<siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" />
</siteMapNode>
</siteMap>
You can use the SiteMapPath control by declaring the control in a page.
The control automatically uses the Web.sitemap file located in the root of your application.
Typically, you add a SiteMapPath control to a Master Page.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Display SiteMapPath</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SiteMapPath
id="SiteMapPath1"
Runat="server" />
<hr />
<h1>Displaying a SiteMapPath Control</h1>
</div>
</form>
</body>
</html>
File: Web.sitemap
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode
url="Default.aspx"
title="Home"
description="The Home Page">
<siteMapNode
url="Default1.aspx"
title="Our Products"
description="Products that we offer">
<siteMapNode
url="FirstProduct.aspx"
title="First Product"
description="The description of the First Product" />
<siteMapNode
url="SecondProduct.aspx"
title="Second Product"
description="The description of the Second Product" />
</siteMapNode>
<siteMapNode
url="Default2.aspx"
title="Our Services"
description="Services that we offer">
<siteMapNode
url="FirstService.aspx"
title="First Service"
description="The description of the First Service"
metaDescription="The first service" />
<siteMapNode
url="SecondService.aspx"
title="Second Service"
description="The description of the Second Service" />
</siteMapNode>
</siteMapNode>
</siteMap>
To display different links to different users depending on their roles, enable the Security Trimming
File: Web.Config
<configuration>
<system.web>
<authentication mode="Windows" />
<roleManager enabled="true" />
<siteMap defaultProvider="MySiteMapProvider">
<providers>
<add
name="MySiteMapProvider"
type="System.Web.XmlSiteMapProvider"
securityTrimmingEnabled="true"
siteMapFile="Web.sitemap" />
</providers>
</siteMap>
</system.web>
</configuration>
Using a template with the SiteMapPath control.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>SiteMapPath Templates</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SiteMapPath
id="SiteMapPath1"
Runat="server">
<NodeTemplate>
<asp:HyperLink
id="lnkPage"
Text="<%# Eval("Title") %>"
NavigateUrl="<%# Eval("Url") %>"
ToolTip="<%# Eval("Description") %>"
Runat="server" />
[<%# Eval("ChildNodes.Count") %>]
</NodeTemplate>
</asp:SiteMapPath>
<hr />
<h1>SiteMapPath Templates</h1>
</div>
</form>
</body>
</html>
File: Web.sitemap
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode
url="Default.aspx"
title="Home"
description="The Home Page">
<siteMapNode
url="Default1.aspx"
title="Our Products"
description="Products that we offer">
<siteMapNode
url="FirstProduct.aspx"
title="First Product"
description="The description of the First Product" />
<siteMapNode
url="SecondProduct.aspx"
title="Second Product"
description="The description of the Second Product" />
</siteMapNode>
<siteMapNode
url="Default2.aspx"
title="Our Services"
description="Services that we offer">
<siteMapNode
url="FirstService.aspx"
title="First Service"
description="The description of the First Service"
metaDescription="The first service" />
<siteMapNode
url="SecondService.aspx"
title="Second Service"
description="The description of the Second Service" />
</siteMapNode>
</siteMapNode>
</siteMap>
Using the SiteMap Class
Under the covers, the SiteMapDataSource control represents the contents of the SiteMap class.
File: Global.asax
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Application_Start(Object sender, EventArgs e)
{
SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
}
SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{
if (SiteMap.CurrentNode == null)
{
string url = e.Context.Request.Path;
string title = Path.GetFileNameWithoutExtension(url);
SiteMapNode newNode = new SiteMapNode(e.Provider, url, url, title);
newNode.ParentNode = SiteMap.RootNode;
return newNode;
}
return SiteMap.CurrentNode;
}
</script>
File: Web.sitemap
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode
url="Default.aspx"
title="Home"
description="The Home Page">
<siteMapNode
url="Default1.aspx"
title="Our Products"
description="Products that we offer">
<siteMapNode
url="FirstProduct.aspx"
title="First Product"
description="The description of the First Product" />
<siteMapNode
url="SecondProduct.aspx"
title="Second Product"
description="The description of the Second Product" />
</siteMapNode>
<siteMapNode
url="Default2.aspx"
title="Our Services"
description="Services that we offer">
<siteMapNode
url="FirstService.aspx"
title="First Service"
description="The description of the First Service"
metaDescription="The first service" />
<siteMapNode
url="SecondService.aspx"
title="Second Service"
description="The description of the Second Service" />
</siteMapNode>
</siteMapNode>
</siteMap>
Using the SiteMapNode Class
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
void Page_Load()
{
if (!Page.IsPostBack)
{
SiteMapNode currentNode = SiteMap.CurrentNode;
this.Title = currentNode.Title;
ltlBodyTitle.Text = currentNode.Title;
lblDescription.Text = currentNode.Description;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>First Product</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1><asp:Literal ID="ltlBodyTitle" runat="server" /></h1>
<asp:Label
id="lblDescription"
Runat="server" />
</div>
</form>
</body>
</html>
File: Web.sitemap
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode
url="Default.aspx"
title="Home"
description="The Home Page">
<siteMapNode
url="Default1.aspx"
title="Our Products"
description="Products that we offer">
<siteMapNode
url="FirstProduct.aspx"
title="First Product"
description="The description of the First Product" />
<siteMapNode
url="SecondProduct.aspx"
title="Second Product"
description="The description of the Second Product" />
</siteMapNode>
<siteMapNode
url="Default2.aspx"
title="Our Services"
description="Services that we offer">
<siteMapNode
url="FirstService.aspx"
title="First Service"
description="The description of the First Service"
metaDescription="The first service" />
<siteMapNode
url="SecondService.aspx"
title="Second Service"
description="The description of the Second Service" />
</siteMapNode>
</siteMapNode>
</siteMap>
using the StartFromCurrentNode property
<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Menu Server Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server"
StartFromCurrentNode="True" />
<asp:Menu ID="Menu1" Runat="server" DataSourceID="SiteMapDataSource1">
</asp:Menu>
</form>
</body>
</html>
File: Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx">
<siteMapNode title="News" description="The Latest News" url="News.aspx">
<siteMapNode title="U.S." description="U.S. News" url="News.aspx?cat=us" />
<siteMapNode title="World" description="World News" url="News.aspx?cat=world" />
<siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" />
<siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" />
</siteMapNode>
<siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" />
</siteMapNode>
</siteMap>
Using the StartingNodeUrl property
<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Menu Server Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server"
StartingNodeUrl="Finance.aspx" />
<asp:Menu ID="Menu1" Runat="server" DataSourceID="SiteMapDataSource1">
</asp:Menu>
</form>
</body>
</html>
File: Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx">
<siteMapNode title="News" description="The Latest News" url="News.aspx">
<siteMapNode title="U.S." description="U.S. News" url="News.aspx?cat=us" />
<siteMapNode title="World" description="World News" url="News.aspx?cat=world" />
<siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" />
<siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" />
</siteMapNode>
<siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" />
</siteMapNode>
</siteMap>
Working with the CurrentNode object (C#)
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = SiteMap.CurrentNode.Description + "<br>" +
SiteMap.CurrentNode.HasChildNodes + "<br>" +
SiteMap.CurrentNode.NextSibling.ToString() + "<br>" +
SiteMap.CurrentNode.ParentNode.ToString() + "<br>" +
SiteMap.CurrentNode.PreviousSibling.ToString() + "<br>" +
SiteMap.CurrentNode.RootNode.ToString() + "<br>" +
SiteMap.CurrentNode.Title + "<br>" +
SiteMap.CurrentNode.Url;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>SiteMapDataSource</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" Runat="server"></asp:Label>
</form>
</body>
File: Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx">
<siteMapNode title="News" description="The Latest News" url="News.aspx">
<siteMapNode title="U.S." description="U.S. News" url="News.aspx?cat=us" />
<siteMapNode title="World" description="World News" url="News.aspx?cat=world" />
<siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" />
<siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" />
</siteMapNode>
<siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" />
</siteMapNode>
</siteMap>
Working with the CurrentNode object (VB)
<%@ Page Language="VB" %>
<script runat="server" language="vb">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = SiteMap.CurrentNode.Description & "<br>" & _
SiteMap.CurrentNode.HasChildNodes & "<br>" & _
SiteMap.CurrentNode.NextSibling.ToString() & "<br>" & _
SiteMap.CurrentNode.ParentNode.ToString() & "<br>" & _
SiteMap.CurrentNode.PreviousSibling.ToString() & "<br>" & _
SiteMap.CurrentNode.RootNode.ToString() & "<br>" & _
SiteMap.CurrentNode.Title & "<br>" & _
SiteMap.CurrentNode.Url
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>SiteMapDataSource</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" Runat="server"></asp:Label>
</form>
</body>
File: Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx">
<siteMapNode title="News" description="The Latest News" url="News.aspx">
<siteMapNode title="U.S." description="U.S. News" url="News.aspx?cat=us" />
<siteMapNode title="World" description="World News" url="News.aspx?cat=world" />
<siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" />
<siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" />
</siteMapNode>
<siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" />
</siteMapNode>
</siteMap>