⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customsitemapprovider.cs

📁 asp。net 2.0宝典一书源码 全书源码给大家共享
💻 CS
字号:
using System;
using System.Configuration.Provider;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Security.Permissions;
using System.Web;

namespace SiteMapDemo
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class CustomSiteMapProvider : SiteMapProvider
    {

        private SiteMapProvider parentProvider = null;
        private string myProviderName = null;
        private string txtFileName = null;
        private SiteMapNode rootNode = null;
        private ArrayList siteNodes = null;
        private ArrayList relationship = null;

        public CustomSiteMapProvider()
        {
            
        }

        //实现当前节点属性
        public override SiteMapNode CurrentNode
        {
            get
            {
                string currentUrl = FindCurrentUrl();
                // 查找代表当前页的节点
                SiteMapNode currentNode = FindSiteMapNode(currentUrl);
                return currentNode;
            }
        }

        //实现根节点属性
        public override SiteMapNode RootNode
        {
            get
            {
                return rootNode;
            }
        }
        //实现父级提供程序
        public override SiteMapProvider ParentProvider
        {
            get
            {
                return parentProvider;
            }
            set
            {
                parentProvider = value;
            }
        }

        //实现根级提供程序
        public override SiteMapProvider RootProvider
        {
            get
            {
                if (this.ParentProvider != null)
                {
                    return ParentProvider.RootProvider;
                }
                else
                {
                    return this;
                }
            }
        }
        // 实现查找地图节点方法
        public override SiteMapNode FindSiteMapNode(string rawUrl)
        {
            if (RootNode.Url == rawUrl)
            {
                return RootNode;
            }
            else
            {
                SiteMapNode candidate = null;
                // 获取匹配的url地址
                lock (this)
                {
                    candidate = GetNode(siteNodes, rawUrl);
                }
                return candidate;
            }
        }
        //实现获取子节点方法
        public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
        {
            SiteMapNodeCollection children = new SiteMapNodeCollection();
            //遍历节点集合,获取有标记的节点作为父节点
            lock (this)
            {
                for (int i = 0; i < relationship.Count; i++)
                {

                    string nodeUrl = ((DictionaryEntry)relationship[i]).Key as string;

                    SiteMapNode parent = GetNode(relationship, nodeUrl);

                    if (parent != null && node.Url == parent.Url)
                    {
                        SiteMapNode child = FindSiteMapNode(nodeUrl);
                        if (child != null)
                        {
                            children.Add(child as SiteMapNode);
                        }
                        else
                        {
                            throw new Exception("错误");
                        }
                    }
                }
            }
            return children;
        }
        protected override SiteMapNode GetRootNodeCore()
        {
            return RootNode;
        }
        // 实现获取父节点方法
        public override SiteMapNode GetParentNode(SiteMapNode node)
        {
            // 检查关系表,如果当前节点没有父节点,则该节点为根节点
            SiteMapNode parent = null;
            lock (this)
            {
                parent = GetNode(relationship, node.Url);
            }
            return parent;
        }

        // 实现初始化方法
        public override void Initialize(string name, NameValueCollection attributes)
        {

            lock (this)
            {

                base.Initialize(name, attributes);

                myProviderName = name;
                txtFileName = attributes["siteMapFile"];
                siteNodes = new ArrayList();
                relationship = new ArrayList();

                // 在内存中构建站点地图
                LoadSiteMapFromStore();
            }
        }
        
        private SiteMapNode GetNode(ArrayList list, string url)
        {
            for (int i = 0; i < list.Count; i++)
            {
                DictionaryEntry item = (DictionaryEntry)list[i];
                if ((string)item.Key == url)
                    return item.Value as SiteMapNode;
            }
            return null;
        }

        private string FindCurrentUrl()
        {
           
            HttpContext currentContext = HttpContext.Current;
            if (currentContext != null)
            {
                return currentContext.Request.RawUrl;
            }
            else
            {
                throw new Exception("HttpContext.Current is Invalid");
            }
        }
        protected virtual void LoadSiteMapFromStore()
        {
            string pathToOpen;

            lock (this)
            {
               
                    // If a root node exists, LoadSiteMapFromStore has already
                    // been called, and the method can return.
                    if (rootNode != null)
                    {
                        return;
                    }
                    else
                    {
                        pathToOpen = HttpContext.Current.Server.MapPath("~" + "\\" + txtFileName);

                        if (File.Exists(pathToOpen))
                        {
                            using (StreamReader sr = File.OpenText(pathToOpen))
                            {
                                rootNode = null;
                                siteNodes.Clear();
                                relationship.Clear();

                                // Parse the file and build the site map
                                string s = "";
                                string[] nodeValues = null;
                                SiteMapNode temp = null;

                                while ((s = sr.ReadLine()) != null)
                                {
                                    nodeValues = s.Split(',');

                                    temp = new SiteMapNode(this,
                                        HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                                        HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                                        nodeValues[1],
                                        nodeValues[2]);

                                    // Is this a root node yet?
                                    if (null == rootNode &&
                                        (null == nodeValues[3] || nodeValues[3] == String.Empty))
                                    {
                                        rootNode = temp;
                                    }
                                    else
                                    {
                                        siteNodes.Add(new DictionaryEntry(temp.Url, temp));
                                        // The parent node has already been added to the collection.
                                        SiteMapNode parentNode =
                                                 FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[3]);
                                        if (parentNode != null)
                                        {
                                            relationship.Add(new DictionaryEntry(temp.Url, parentNode));
                                        }
                                        else
                                        {
                                            throw new Exception("错误");
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("文件没有找到");
                        }

                    }
               
            }
            return;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -