现在的位置: 首页 > 综合 > 正文

ASP.NET2.0自动搜索文件组成导航系统

2013年04月02日 ⁄ 综合 ⁄ 共 5574字 ⁄ 字号 评论关闭

ASP.NET2.0的导航系统确实给web开发带来方便,但是用过的用户就会发现导航系统有一个很大的缺陷:他需要你手工编写web.sitemap,web.sitemap的语法是“相当的简单”,但是实际运用时,虽然简单,对于稍微复杂的导航,你肯定容易出错。下面是一个简单的sitemap,
 
<siteMap>
 <siteMapNode title="Home" url="~/default.aspx" >
    <siteMapNode title="Introduction to ASP.NET" url="~/introduction/default.aspx">
    <siteMapNode title="What's New in Whidbey?" url="~/introduction/whatsnew.aspx"/>
      <siteMapNode title="Sample Applications (Starter Kits)" url="~/introduction/starterkits.aspx"/>
      <siteMapNode title="Introduction to Visual Web Developer" url="~/introduction/vwd.aspx"/>
    </siteMapNode>
    <siteMapNode title="Building A Web Application" url="~/development/default.aspx">
      <siteMapNode title="Building a Simple Application" url="~/development/simple/default.aspx">
        <siteMapNode title="Introduction to ASP.NET pages" url="~/development/simple/pages.aspx"/>
        <siteMapNode title="Introduction to Server Controls" url="~/development/simple/servercontrols.aspx"/>
        <siteMapNode title="Inline vs Code Behind Pages" url="~/development/simple/codeseparation.aspx"/>
        <siteMapNode title="Sharing Code Between Pages" url="~/development/simple/codedirectory.aspx"/>
      </siteMapNode>
</siteMap>
说白了,他只是一些siteMapNode 的嵌套,但是嵌套的开闭呼应对人而言,是一个烦点,但是对计算机来说,就喜欢处理这些简单的关系,所以我们可以编写一个文件,让系统自动检索当前应用程序的页面并自动生成导航。
首先定义一些变量
        //私有成员
        private SiteMapNode _root;
        private String _rootTitle = "Home";
        private string _rootUrl = "~/Default.aspx";
 
        private bool _useDefaultPageAsFolderUrl = true;
        private string _defaultPageName = "default.aspx";
        private CacheDependency _fsMonitor;
 
        private StringDictionary _excludeFileList;   
        private StringDictionary _excludeFolderList;
        private char[] _listSeparator ={ ',' };
     
在这些变量中,_root变量存放站点的根目录,另外定义了_rootTitle和_rootUrl,使用这两个变量存放根目录的连接标题和链接地址。
 你可能发现我还定义了CacheDependency 类型的_fsMonitor,在本导航里,使用了缓存技术而且强烈推荐你使用缓存,因为站点导航将来在各个页面使用,如果每次都由系统自动根据文件列表生成导航,那么将明显影响性能,所以这里使用了缓存,基本思想是:如果用户没有更改站点下的页面,就直接从缓存里获取导航信息,否则,就重新生成站点导航,生成新导航后,同样会复制一份副本到到当前缓存里。
 
 在上面的变量里还有两个变量:_excludeFileLis和_excludeFolderList,顾名思义,这两个变量表示将来我想要排斥在导航里的文件和文件夹。什么意思呢?
 由于我们是让系统枚举根目录下的所有文件,所以系统将默认根据该目录下的所有文件生成所有站点导航,这显然并不能够完全满足我们的需求。
在后面实现的代码里,你可以发现如下一段代码:
 
            String folder = HttpContext.Current.Server.MapPath(folderPath);
            DirectoryInfo dirInfo = new DirectoryInfo(folder);
            foreach (FileInfo fi in dirInfo.GetFiles("*.aspx"))
            {
                SiteMapNode fileNode = CreateFileNode(fi.FullName, parentNode, folderPath);
                if (fileNode != null)
                    AddNode(fileNode, parentNode);
            }
 
            foreach (DirectoryInfo di in dirInfo.GetDirectories())
            {
               SiteMapNode folderNode = CreateFolderNode(di.FullName, String.Concat(folderPath,
di.Name, "/") + DefaultPageName);
                if (folderNode != null)
                    AddNode(folderNode, parentNode);
 
 
                BuildSiteMapFromFileSystem(folderNode, String.Concat(folderPath, di.Name, "/"));
           
       }
 
这段代码显示了生成的导航其实是枚举每一个文件夹下的aspx页面,但是可能有时候我们并不希望他枚举所有目录下的aspx页面,(包括App_Code, images,等,如果有的话)以及其他为了安全而不想让系统显示的aspx页面,
 _excludeFileLis和_excludeFolderList就可能让我们告诉系统哪些文件应该排除在导航里。
_excludeFileLis和_excludeFolderList的值来自于web.config,你可以在web.config里增加如下配置:
        <add name="FileSystemSiteMapProvider"
            type="FileSystemSiteMapProvider"
            ExcludeFileList="default1.aspx,default2.aspx"
ExcludeFolderList="myfiles,myfile2"
/>
通过对web.config里对外公开两个排斥文件的接口,就可以让客户轻而易举的配置导航实际生成的需求。
     
 在代码里可以看到如下一句代码:
       public void Refresh()
        {
            _root = null;
            base.Clear();
        }
 前面曾经说过,为了提高性能,使用了缓存技术,当缓存失效以后,就会调用Refresh刷新系统,这个失效是是通过调用base.Clear();来实现的
 
在构建站点导航时,是利用BuildSiteMap函数完成,这是实现导航的核心函数,该函数的代码如下
 
 public override System.Web.SiteMapNode BuildSiteMap()
        {
            lock (this)
            {
                if (_root != null)
                    if (!_fsMonitor.HasChanged)
                        return _root;
 
                Refresh();
 
                _root = CreateFolderNode(HttpContext.Current.Server.MapPath(RootUrl), RootUrl);
                _root.Title = RootTitle;
                _fsMonitor = new CacheDependency(HttpContext.Current.Server.MapPath("~/"));
                AddNode(_root);
                BuildSiteMapFromFileSystem(_root, "~/");
                return _root;
            }
你可以发现在这里使用了
lock()
{
}
进行锁定,因为我们希望系统在更新时实行类似“数据库里事务”的操作,一次更新要么全做,要么全不做,最根本的原因是因为用户请求的并发性。
 
 在这个导航里使用了自定义的Provider模型,为了简化开发,我们使用了的FileSystemSiteMapProvider 类,该类从StaticSiteMapProvider类派生,这也是微软推荐的一种模式。现在在web.config里添加如下配置
 <siteMap enabled="true" defaultProvider="FileSystemSiteMapProvider">
      <providers>
        <add name="FileSystemSiteMapProvider"
            type="FileSystemSiteMapProvider"
             />
      </providers>
    </siteMap>
就可以了
下面是某一个页面(*.aspx)使用它的基本模式
  <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1"
ImageSet="XPFileExplorer"
                NodeIndent="15" ShowLines="True" MaxDataBindDepth="5">
                <ParentNodeStyle Font-Bold="False" />
                <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
                <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False"
HorizontalPadding="0px"
                    VerticalPadding="0px" />
                <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black"
HorizontalPadding="2px"
                    NodeSpacing="0px" VerticalPadding="2px" />
            </asp:TreeView>
     <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
 
正如你所看到的,使用步骤如下
1)在aspx页面放置一个asp:SiteMapDataSource,无须任何设置
2)然后使用treeview(menu,...)设置其datasourceid为SiteMapDataSource1即可。
 
就这么简单,顺便说一下,如果你使用后,突然又想使用默认的web.sitemap文件,作为最简单的处理方式,只要更改web.config里的defaultProvider为XmlSitemap即可
 
本文愿代码下载与说明
此处是完成源代码,如果你需要在你的项目里使用,只要把该文件copy到应用程序的App_Code目录下即可(扩展名请更改为cs文件)。

抱歉!评论已关闭.