第一,发现这个一旦生成sitemap以后如果服务器不重启,那么即使你修改了表内容也无法实时的表现出结果来。原因是做个一个SMNode是否为空判断。
不知道这里的这个判断有什么实际的意义?我发现一旦生成这个sitemap后这个东西就一直随着服务器存在。
第二,如何和我自己的权限配合起来用,好像记得这个有个权限的功能。不过不知道是不是需要和membership配合使用?如果我根据自己的权限来对不同用户生成sitemap不知是否可行?会出什么问题?
第三,数据库如果有个链接是直接链接到其他网站的,会出错。比如 http://www.sohu.com 但是有些时候确实需要这样的链接,这个怎么加进去?
第四,如果使用这种方式如何设置多个sitemap?看了一下msdn,视乎只有用 XmlSiteMapProvider 才能设置多个?
下面是我改的代码。^_^
namespace
LILELTP.Controls
{
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;
//引入需要的命名空间
using System.Collections.Specialized;
using System.Collections;
using System.Security.Permissions;
using db=LILELTP.DataBase.Database;
/**//**/
/**//// <summary>
/// sqlsitemap 的摘要说明
/// </summary>
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
//权限说明
public class sqlsitemap : StaticSiteMapProvider
{
private SiteMapNode SMNode = null;
private string time = null;
public sqlsitemap()
{
}
private bool initialized = false;
public virtual bool IsInitialized
{//重写基类IsInitialized属性,在调用基类 Initialize 之后在该方法中执行自己的初始化
get
{ return initialized; }
}
protected override SiteMapNode GetRootNodeCore()
{//重写基类GetRootNodeCore方法,使之返回RootNode这个属性
return BuildSiteMap();
}
public override void Initialize(string name, NameValueCollection attributes)
{//重写基类Initialize方法,如果成功,那么将initialized设置为true
if (IsInitialized)
{
return;
}
base.Initialize(name, attributes);
//调用基类Initialize方法来初始化
initialized = true;
}
protected override void Clear()
{
lock (this)
{
if (SMNode == null)
base.Clear();
}
}
public override SiteMapNode BuildSiteMap()
{
lock (this)
{//锁定
if (!IsInitialized)
{//判断是否初始化了
throw new Exception("数据未初始化");
}
else
{
if (SMNode == null)
{
Clear();
string strSQL = "SELECT ID,Name,ParentID,NavigateUrl,description from might_Menu WHERE Purview='Y' ORDER BY [ID],ParentID";
DataSet dsnode = db.ExecuteDataset(db.ConnectionString, CommandType.Text, strSQL);
SMNode = new SiteMapNode(this, "-1", "default.aspx", "首页");
BuildChildSite(ref SMNode, 0, dsnode);
time = DateTime.Now.ToString();
}
}
string x = time;
return SMNode;
}
}
/**//// <summary>
/// 遍历生成子节点菜单
/// </summary>
/// <param name="smn">父节点</param>
/// <param name="cid">节点编号</param>
/// <param name="ds">DataSet</param>
private void BuildChildSite(ref SiteMapNode smn, int pid, DataSet ds)
{
try
{
foreach (DataRow dr in ds.Tables[0].Select("ParentID = " + pid))
{
SiteMapNode nsmn = new SiteMapNode(this, dr["id"].ToString(), dr["NavigateUrl"].ToString(), dr["Name"].ToString(), dr["description"].ToString());
AddNode(nsmn, smn);
BuildChildSite(ref nsmn, (int)dr["id"], ds);
}
}
catch (Exception ex)
{
ex.ToString();
}
}
}
}
{
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;
//引入需要的命名空间
using System.Collections.Specialized;
using System.Collections;
using System.Security.Permissions;
using db=LILELTP.DataBase.Database;
/**//**/
/**//// <summary>
/// sqlsitemap 的摘要说明
/// </summary>
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
//权限说明
public class sqlsitemap : StaticSiteMapProvider
{
private SiteMapNode SMNode = null;
private string time = null;
public sqlsitemap()
{
}
private bool initialized = false;
public virtual bool IsInitialized
{//重写基类IsInitialized属性,在调用基类 Initialize 之后在该方法中执行自己的初始化
get
{ return initialized; }
}
protected override SiteMapNode GetRootNodeCore()
{//重写基类GetRootNodeCore方法,使之返回RootNode这个属性
return BuildSiteMap();
}
public override void Initialize(string name, NameValueCollection attributes)
{//重写基类Initialize方法,如果成功,那么将initialized设置为true
if (IsInitialized)
{
return;
}
base.Initialize(name, attributes);
//调用基类Initialize方法来初始化
initialized = true;
}
protected override void Clear()
{
lock (this)
{
if (SMNode == null)
base.Clear();
}
}
public override SiteMapNode BuildSiteMap()
{
lock (this)
{//锁定
if (!IsInitialized)
{//判断是否初始化了
throw new Exception("数据未初始化");
}
else
{
if (SMNode == null)
{
Clear();
string strSQL = "SELECT ID,Name,ParentID,NavigateUrl,description from might_Menu WHERE Purview='Y' ORDER BY [ID],ParentID";
DataSet dsnode = db.ExecuteDataset(db.ConnectionString, CommandType.Text, strSQL);
SMNode = new SiteMapNode(this, "-1", "default.aspx", "首页");
BuildChildSite(ref SMNode, 0, dsnode);
time = DateTime.Now.ToString();
}
}
string x = time;
return SMNode;
}
}
/**//// <summary>
/// 遍历生成子节点菜单
/// </summary>
/// <param name="smn">父节点</param>
/// <param name="cid">节点编号</param>
/// <param name="ds">DataSet</param>
private void BuildChildSite(ref SiteMapNode smn, int pid, DataSet ds)
{
try
{
foreach (DataRow dr in ds.Tables[0].Select("ParentID = " + pid))
{
SiteMapNode nsmn = new SiteMapNode(this, dr["id"].ToString(), dr["NavigateUrl"].ToString(), dr["Name"].ToString(), dr["description"].ToString());
AddNode(nsmn, smn);
BuildChildSite(ref nsmn, (int)dr["id"], ds);
}
}
catch (Exception ex)
{
ex.ToString();
}
}
}
}
if
exists
(
select
1
from sysobjects
where id = object_id ( ' dbo.Menu ' )
and type = ' U ' )
drop table dbo.Menu
go
/**/ /*==============================================================*/
/**/ /* Table: Menu */
/**/ /*==============================================================*/
create table dbo.Menu (
ID int not null ,
Name varchar ( 20 ) null ,
ParentID int null ,
NavigateUrl varchar ( 100 ) null ,
description varchar ( 200 ) null ,
Taxis varchar ( 20 ) null ,
Purview char ( 1 ) null ,
DelSign int null ,
constraint PK_MENU primary key (ID)
)
go
from sysobjects
where id = object_id ( ' dbo.Menu ' )
and type = ' U ' )
drop table dbo.Menu
go
/**/ /*==============================================================*/
/**/ /* Table: Menu */
/**/ /*==============================================================*/
create table dbo.Menu (
ID int not null ,
Name varchar ( 20 ) null ,
ParentID int null ,
NavigateUrl varchar ( 100 ) null ,
description varchar ( 200 ) null ,
Taxis varchar ( 20 ) null ,
Purview char ( 1 ) null ,
DelSign int null ,
constraint PK_MENU primary key (ID)
)
go
发现msdn上有这么一篇文章
http://msdn2.microsoft.com/en-us/library/aa479033.aspx