自定义SiteMapProvider 转

因为我的做的一个项目是基于Category分类来进行导航的, 查看 ASP.NET 站点导航概述t.gif 和  如何:实现 ASP.NET 站点地图提供程序t.gif  两篇参考文章, 也模仿地写了一个SiteMapProvider程序....

各个 ASP.NET 站点导航组件之间的关系图
站点导航类之间的关系

而我们这次就是编程图中的"自定义站点地图提供程序"....

我的DAL层是基于NBear的....
Category实体定义如下:
[AutoPreLoad]
    
public   interface  Category : Entity
    {
        [PrimaryKey]
        
int  ID {  get ; }

        [SqlType(
" nvarchar(50) " )]
        
string  Title {  get set ; }

        
///   <summary>
        
///  排序位置
        
///   </summary>
         int  Sort {  get ; set ;}

        [FkReverseQuery(LazyLoad 
=   true )]
        [MappingName(
" ParentID " )]
        Category Parent { 
get ; set ;}

        [FkQuery(
" Parent " , LazyLoad  =   false , OrderBy =   " Sort " )]
        [SerializationIgnore]
        Category[] Children { 
get set ; }

        [FkQuery(
" Category " , OrderBy  =   " {ID} DESC " , LazyLoad  =   true )]
        Article[] Articles { 
get ; set ;}
    }


程序代码如下:
ContractedBlock.gif ExpandedBlockStart.gif MK2.AspNet.Controls.AccessSiteMapProvider
namespace MK2.AspNet.Controls
{

    
using System;
    
using System.Collections;
    
using System.Collections.Specialized;
    
using System.Data;
    
using System.Data.OleDb;
    
using System.Security.Permissions;
    
using System.Web;
    
using Entities;

    
/// An extremely simple AccessSiteMapProvider that only supports a
    
/// site map node hierarchy 1 level deep.
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    
public class AccessSiteMapProvider : StaticSiteMapProvider
    {
        
private SiteMapNode rootNode = null;

        
private CategoryDAL categoryDAL = null;

        
// Implement a default constructor.
        public AccessSiteMapProvider() { }

        
// Some basic state to help track the initialization state of the provider.
        private bool initialized = false;
        
public virtual bool IsInitialized
        {
            
get
            {
                
return initialized;
            }
        }
        
// Return the root node of the current site map.
        public override SiteMapNode RootNode
        {
            
get
            {
                SiteMapNode temp 
= null;
                temp 
= BuildSiteMap();
                
return temp;
            }
        }
        
protected override SiteMapNode GetRootNodeCore()
        {
            
return RootNode;
        }
        
// Initialize is used to initialize the properties and any state that the
        
// AccessProvider holds, but is not used to build the site map.
        
// The site map is built when the BuildSiteMap method is called.
        public override void Initialize(string name, NameValueCollection attributes)
        {
            
if (IsInitialized)
                
return;

            
base.Initialize(name, attributes);

            
// Create and test the connection to the Microsoft Access database.
            categoryDAL = new CategoryDAL();

            initialized 
= true;
        }

        
///
        
/// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
        
///
        // Clean up any collections or other state that an instance of this may hold.
        protected override void Clear()
        {
            
lock (this)
            {
                rootNode 
= null;
                
base.Clear();
            }
        }

        
// Build an in-memory representation from persistent
        
// storage, and return the root node of the site map.
        public override SiteMapNode BuildSiteMap()
        {

            
// Since the SiteMap class is static, make sure that it is
            
// not modified while the site map is built.
            lock (this)
            {

                
// If there is no initialization, this method is being
                
// called out of order.
                if (!IsInitialized)
                {
                    
throw new Exception("BuildSiteMap called incorrectly.");
                }

                
// If there is no root node, then there is no site map.
                if (null == rootNode)
                {
                    
// Start with a clean slate
                    Clear();

                    
// Select the root node of the site map from Microsoft Access.

                    rootNode 
= new SiteMapNode(this"-1""~/""首页""Home page.");

                    Category[] childNodes 
= categoryDAL.GetCategories(0);
                    
foreach (Category child in childNodes)
                    {
                        SiteMapNode childNode;
                        childNode 
= GetNode(child);
                        AddNode(childNode, rootNode);
                    }
                }
                
return rootNode;
            }
        }

        
private SiteMapNode GetNode(Category node)
        {
            SiteMapNode returnNode 
= new SiteMapNode(this, node.ID.ToString(),
                    
"~/Category.aspx?Category=" + node.ID, node.Title, node.Title);
            
if (node.Children.Count > 0)
            {
                
foreach (Category child in node.Children)
                {
                    SiteMapNode childNode;
                    childNode 
= GetNode(child);
                    AddNode(childNode, returnNode);
                }
            }
            
return returnNode;
        }
    }
}


此SiteMapProvider只适合我的项目, 在此只是作个示例展示如何扩展StaticSiteMapProvider而已....
posted on 2007-05-20 15:58 MK2t.gif 阅读(49) 评论(2)   编辑  收藏 引用 网摘 所属分类: 1 .NET 2.0/1.1 & 3.X7 NBear 学习之路8 DataBase
753243.html?webview=1

FeedBack:
#  re: 自定义SiteMapProvider[未登录]
2007-05-28 22:41 | 陈亮
请问自定义后 web.config文件怎么配置?
如果SQL SERVER的数据库又怎么配置呢?   回复   更多评论
  
#  re: 自定义SiteMapProvider
2007-06-03 17:32 | MK2t.gif
Web.config中的设置, 示例如下:
<system.web>
<siteMap defaultProvider="AccessSiteMapProvider">
<providers>
<add
name="AccessSiteMapProvider"
type="MK2.AspNet.Controls.AccessSiteMapProvider" />
</providers>
</siteMap>   回复   更多评论
  

转载于:https://www.cnblogs.com/kagar/archive/2007/06/04/771286.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值