在SiteMap中写入固定节点:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < siteMap xmlns ="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
3 < siteMapNode url ="Forum.aspx" title ="FLT Forum" description ="" >
4 < siteMapNode url ="Topic.aspx" title ="帖子列表" description ="" >
5 < siteMapNode url ="Post.aspx" title ="查看帖子" description ="" />
6 </ siteMapNode >
7 </ siteMapNode >
8 </ siteMap >
基本上这个只标识一下网站结构
2 < siteMap xmlns ="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
3 < siteMapNode url ="Forum.aspx" title ="FLT Forum" description ="" >
4 < siteMapNode url ="Topic.aspx" title ="帖子列表" description ="" >
5 < siteMapNode url ="Post.aspx" title ="查看帖子" description ="" />
6 </ siteMapNode >
7 </ siteMapNode >
8 </ siteMap >
在master页的page_load中注册事件:
1
protected
void
Page_Load(
object
sender, EventArgs e)
2 {
3 if ( ! IsPostBack)
4 {
5 SiteMap.SiteMapResolve += new SiteMapResolveEventHandler( this .ForumPath);
6 }
7 }
事件处理代码:
2 {
3 if ( ! IsPostBack)
4 {
5 SiteMap.SiteMapResolve += new SiteMapResolveEventHandler( this .ForumPath);
6 }
7 }
SiteMapNode ForumPath(
object
sender, SiteMapResolveEventArgs e)
{
SiteMapNode currNode = SiteMap.CurrentNode.Clone(true);//克隆当前节点,true表示克隆所有父节点
SiteMapNode tempNode = currNode;
string strClassName = "";//论坛分类名,这个最后我没用上,不过要使SiteMap更人性化的话,加上还是好一些.
string strTopName = ""; //板块名
string strPostTitle = "";//帖子主题
string strForumID = getForumID(out strClassName);//3个方法后面我会提到
string strTopID = getTopID(out strTopName);
string strPostID = getPostID(out strPostTitle);
if (strForumID != "" && strTopID != "")//因为结构很简单,只有3层,当ForumID和TopID同时存在于url参数中的时候必然是帖子列表
{
tempNode.Title = strTopName;//设定帖子列表节点的title
if (strForumID != "" && strTopID != "" && strPostID != "")//当3个参数同时存在的时候,当前位置肯定是帖子内容
{
if ((tempNode = tempNode.ParentNode) != null)//将父节点存为当前节点
{
tempNode.Url = tempNode.Url + "?ForumID=" + strForumID + "&TopID=" + strTopID;//设定当前节点(帖子内容的父节点就是帖子列表)的url
tempNode.Title = strTopName;设定帖子列表的title
tempNode.ChildNodes[0].Title = strPostTitle;设定帖子内容的title
}
}
}
return currNode;//返回节点
}
{
SiteMapNode currNode = SiteMap.CurrentNode.Clone(true);//克隆当前节点,true表示克隆所有父节点
SiteMapNode tempNode = currNode;
string strClassName = "";//论坛分类名,这个最后我没用上,不过要使SiteMap更人性化的话,加上还是好一些.
string strTopName = ""; //板块名
string strPostTitle = "";//帖子主题
string strForumID = getForumID(out strClassName);//3个方法后面我会提到
string strTopID = getTopID(out strTopName);
string strPostID = getPostID(out strPostTitle);
if (strForumID != "" && strTopID != "")//因为结构很简单,只有3层,当ForumID和TopID同时存在于url参数中的时候必然是帖子列表
{
tempNode.Title = strTopName;//设定帖子列表节点的title
if (strForumID != "" && strTopID != "" && strPostID != "")//当3个参数同时存在的时候,当前位置肯定是帖子内容
{
if ((tempNode = tempNode.ParentNode) != null)//将父节点存为当前节点
{
tempNode.Url = tempNode.Url + "?ForumID=" + strForumID + "&TopID=" + strTopID;//设定当前节点(帖子内容的父节点就是帖子列表)的url
tempNode.Title = strTopName;设定帖子列表的title
tempNode.ChildNodes[0].Title = strPostTitle;设定帖子内容的title
}
}
}
return currNode;//返回节点
}
下面是3个方法,返回ForumID,TopID,PostID3个参数
1
private
string
getForumID(
out
string
strClassName)
2 {
3 string strForumID = "" ;
4 if (System.Web.HttpContext.Current.Request.QueryString[ " ForumID " ] != null ) // 这里必须要写System.Web.HttpContext.Current.Request.QueryString,而不能直接写Request.QueryString,这个问题在csdn上很长时间没解决啊,后来在msdn上一位朋友指点.
5 {
6 strForumID = System.Web.HttpContext.Current.Request.QueryString[ " ForumID " ].ToString();
7 strClassName = xxx; // 通过数据库或者Dataset获取分类名
8 return strForumID;
9 }
10 else
11 {
12 strPartName = "" ;
13 return strForumID;
14 }
15
16 }
17
18 private string getTopID( out string strTopName)
19 {
20 string strTopID = "" ;
21
22 if (System.Web.HttpContext.Current.Request.QueryString[ " TopID " ] != null )
23 {
24 strTopID = System.Web.HttpContext.Current.Request.QueryString[ " TopID " ].ToString();
25 strTopName = xxx; // 同上
26 return strTopID;
27 }
28 else
29 {
30 strTopName = "" ;
31 return strTopID;
32 }
33 }
34
35 private string getPostID( out string strPostTitle)
36 {
37 string strPostID = "" ;
38
39 if (System.Web.HttpContext.Current.Request.QueryString[ " PostID " ] != null )
40 {
41 strPostID = System.Web.HttpContext.Current.Request.QueryString[ " PostID " ].ToString();
42 strPostTitle = xxx; // 同上
43 return strPostID;
44 }
45 else
46 {
47 strPostTitle = "" ;
48 return strPostID;
49 }
50 }
2 {
3 string strForumID = "" ;
4 if (System.Web.HttpContext.Current.Request.QueryString[ " ForumID " ] != null ) // 这里必须要写System.Web.HttpContext.Current.Request.QueryString,而不能直接写Request.QueryString,这个问题在csdn上很长时间没解决啊,后来在msdn上一位朋友指点.
5 {
6 strForumID = System.Web.HttpContext.Current.Request.QueryString[ " ForumID " ].ToString();
7 strClassName = xxx; // 通过数据库或者Dataset获取分类名
8 return strForumID;
9 }
10 else
11 {
12 strPartName = "" ;
13 return strForumID;
14 }
15
16 }
17
18 private string getTopID( out string strTopName)
19 {
20 string strTopID = "" ;
21
22 if (System.Web.HttpContext.Current.Request.QueryString[ " TopID " ] != null )
23 {
24 strTopID = System.Web.HttpContext.Current.Request.QueryString[ " TopID " ].ToString();
25 strTopName = xxx; // 同上
26 return strTopID;
27 }
28 else
29 {
30 strTopName = "" ;
31 return strTopID;
32 }
33 }
34
35 private string getPostID( out string strPostTitle)
36 {
37 string strPostID = "" ;
38
39 if (System.Web.HttpContext.Current.Request.QueryString[ " PostID " ] != null )
40 {
41 strPostID = System.Web.HttpContext.Current.Request.QueryString[ " PostID " ].ToString();
42 strPostTitle = xxx; // 同上
43 return strPostID;
44 }
45 else
46 {
47 strPostTitle = "" ;
48 return strPostID;
49 }
50 }
总结一下,这个方法对于结构比较简单(主要是深度比较小的)的网站,个人感觉要比操作XML文件简单,直接写程序就可以实现动态修改了,但是对于结构比较复杂(深度比较大的)网站,还是写XML比较直接.