因为同一个理由,VS的控件有很多无用的代码,而且并满足不了开发的需求,所以先写了一个轻量级的SiteMap控件顶着.
原理其实很简单,就是读取配置文件,加载到高速缓存,然后使用XMLDocument进行解析,同时可以满足三个要求:1.Site Map Path; 2.Site Map Menu; 3.Page Title.而XML配置文件的格式是按照页面文件名作为标签命名方式,所以查找起来就可以节省不少的效率.
下面是控件代码:
程序代码
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.Web.Caching;
using System.Xml;
using System.Text;
/// <summary>
/// 自定义站点地图
/// </summary>
public class ExSiteMap
{
private HttpContext myHttp = HttpContext.Current;
private XmlDocument myDoc = null;
private string CacheKey;
private string XmlPath;
private XmlNodeList myXmlNode;
/// <summary>
/// 设置SiteMap
/// <para><param name="CacheKey">高速缓存名称</param></para>
/// <para><param name="XmlPath">站点配置文件路径</param></para>
/// <para><param name="CacheTime">高速缓存时间 单位:小时</param></para>
/// </summary>
public ExSiteMap(string CacheKey, string XmlPath,DateTime CacheTime)
{
this.CacheKey = CacheKey;
this.XmlPath = XmlPath;
if (this.myHttp.Cache[CacheKey] == null)
{
myDoc = new XmlDocument();
myDoc.Load(XmlPath);
myHttp.Cache.Insert(CacheKey, myDoc, null, CacheTime, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
myDoc = (XmlDocument)myHttp.Cache[CacheKey];
//得到当前URL名称
string[] myUrlFile = myHttp.Request.Path.Split(new Char[] { Convert.ToChar("/"), Convert.ToChar(".") });
string myUrl = myUrlFile[myUrlFile.Length - 2];
myXmlNode = myDoc.GetElementsByTagName(myUrl);
}
/// <summary>
/// 生成站点当前位置
/// </summary>
public string SiteMapPath()
{
StringBuilder myStringBuilder = new StringBuilder("");
if (myXmlNode.Count > 0)
{
XmlNode thisNode = myXmlNode[myXmlNode.Count-1];
myStringBuilder.Append(thisNode.Attributes["title"].InnerText);
while (thisNode.LocalName != "Root")
{
StringBuilder newStringBuilder = new StringBuilder("<a href=\"")
.Append(thisNode.ParentNode.Attributes["url"].InnerText).Append("\" title=\"")
.Append(thisNode.ParentNode.Attributes["description"].InnerText).Append("\">")
.Append(thisNode.ParentNode.Attributes["title"].InnerText).Append("</a> » ");
myStringBuilder.Insert(0, newStringBuilder);
thisNode = thisNode.ParentNode;
}
}
return myStringBuilder.ToString();
}
/// <summary>
/// 生成站点菜单导航
/// </summary>
public string SiteMapMenu()
{
StringBuilder myStringBuilder = new StringBuilder("");
if (myXmlNode.Count > 0)
{
XmlNode thisNode = myXmlNode[myXmlNode.Count - 1];
XmlNodeList Siblings = thisNode.ParentNode.ChildNodes;
for (int i = 0, j = Siblings.Count; i < j; i++)
{
myStringBuilder.Append("<li><a href=\"").Append(Siblings[i].Attributes["url"].InnerText)
.Append("\" title=\"").Append(Siblings[i].Attributes["description"].InnerText)
.Append("\"");
if (thisNode == Siblings[i])
{
myStringBuilder.Append(" class=\"choose\"");
}
myStringBuilder.Append("><span>").Append(Siblings[i].Attributes["title"].InnerText)
.Append("</span></a></li>\r\n");
}
}
return myStringBuilder.ToString();
}
/// <summary>
/// 生成站点Title
/// </summary>
public string SiteTitle()
{
string myTitle = "";
if (myXmlNode.Count > 0)
{
XmlNode thisNode = myXmlNode[myXmlNode.Count - 1];
myTitle = thisNode.Attributes["title"].InnerText;
}
return myTitle;
}
}
下面是配置文件:
程序代码
<?xml version="1.0" encoding="utf-8" ?>
<Root url="site_info.aspx" title="后台管理中心" description="后台管理中心">
<SubRoot url="site_info.aspx" title="后台基本设置" description="后台基本设置">
<site_info url="site_info.aspx" title="站点基本信息" description="站点基本信息" />
<site_serve_info url="site_serve_info.aspx" title="服务器信息" description="服务器信息" />
</SubRoot>
<SubRoot url="site_set.aspx" title="站点基本设置" description="站点基本设置">
<site_set url="site_set.aspx" title="站点设置" description="站点设置" />
</SubRoot>
<SubRoot url="site_category.aspx" title="分类管理" description="分类管理">
<site_category url="site_category.aspx" title="内容分类设置" description="内容分类设置" />
<site_category_add url="site_category_add.aspx" title="添加内容分类" description="添加内容分类" />
<site_judge url="site_judge.aspx" title="评判分类管理" description="评判分类管理" />
<site_tag url="site_tag.aspx" title="Tag分类管理" description="Tag分类管理" />
</SubRoot>
</Root>
调用方法:
程序代码
ExSiteMap mySiteMap = new ExSiteMap("SiteMapCache", Request.MapPath("sitemap.xml"), DateTime.Now.AddHours(1));
Page.Header.Title = mySiteMap.SiteTitle();
SiteMap.InnerHtml = mySiteMap.SiteMapPath();
SiteMenu.InnerHtml = mySiteMap.SiteMapMenu();
因为没有使用微软的web.sitemap文件,所以这个配置问题会出现安全问题,等有时间再搞个高深点的.而且目前还有没去写对于用户角色管理的权限,所以还是很轻量级的.