首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> ASP.NET技巧 -> 使用asp.net2.0中的SiteMap中的一些问题
使用asp.net2.0中的SiteMap中的一些问题


文章类别:ASP.NET技巧 来源: 作者: 发表日期:2006-3-18 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

SiteMap,网站地图,在网站建设的时候是很有用的。它可以直接绑定在Men和TreeView控件上,还有一个指示当前路径的SiteMapPath控件,也可以直接绑定。
这是他常用的xml定义:
  <siteMapNode url="Course/Group/GroupList.aspx" title="GroupAdmin" >
这个SiteMap的权限已经和Membership结合起来了,不同权限的用户所看到的地图已经被控制了。可以配置role属性来扩展例外的访问许可。注意,是例外的访问许可。
<siteMapNode url="Course/Tests/TestList.aspx" title="TestAdmin" role="student">这里有些介绍:http://zmsx.cnblogs.com/archive/2006/01/03/310381.aspx

简单的使用这里不作赘述,只是讨论一下怎么和扩展一下,让他可以访问资源时附带参数。

首先介绍这样一个资源:MySiteMapTool:http://quitgame.cnblogs.com/archive/2005/11/24/283910.aspx
这位仁兄已经提供了一个工具,可以在程序中转发带参数的请求
比如: MySiteMap.Forward("Details", "AlbumID={0}&Page={1}", 1, 4);
确是简单实用。

现在想要的功能是:因为各个液面都需要不同的参数,所以在没有这些参数的情况下就禁止用户访问那个页面,转而访问父一级页面,递归。

首先,SiteMap本身有个SiteMapResolve事件,在当前路径被解析时触发,这是一段来自MSDN的代码

private void Page_Load(object sender, EventArgs e)
{
    // The ExpandForumPaths method is called to handle
    // the SiteMapResolve event.
    SiteMap.SiteMapResolve +=
      new SiteMapResolveEventHandler(this.ExpandForumPaths);
}

private SiteMapNode ExpandForumPaths(Object sender, SiteMapResolveEventArgs e)
{
    // The current node represents a Post page in a bulletin board forum.
    // Clone the current node and all of its relevant parents. This
    // returns a site map node that a developer can then
    // walk, modifying each node.Url property in turn.
    // Since the cloned nodes are separate from the underlying
    // site navigation structure, the fixups that are made do not
    // effect the overall site navigation structure.
    SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
    SiteMapNode tempNode = currentNode;

    // Obtain the recent IDs.
    int forumGroupID = GetMostRecentForumGroupID();
    int forumID = GetMostRecentForumID(forumGroupID);
    int postID = GetMostRecentPostID(forumID);

    // The current node, and its parents, can be modified to include
    // dynamic querystring information relevant to the currently
    // executing request.
    if (0 != postID)
    {
        tempNode.Url = tempNode.Url + "?PostID=" + postID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
        (0 != forumID))
    {
        tempNode.Url = tempNode.Url + "?ForumID=" + forumID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
        (0 != forumGroupID))
    {
        tempNode.Url = tempNode.Url + "?ForumGroupID=" + forumGroupID.ToString();
    }

    return currentNode;
}

 

这段代码只是给当前路径加载参数。

曾经尝试过使用类似的方法,但是SiteMapPath搞定了,Menu就绑定不上数据了。并且只能处理一部分数据。


后来,结合SiteMapTool那个类,又写出几个函数可以解决这个问题
这是修改之后的sitemap文件,加了一个配置项:rule,里面的参数是这个页面需要的参数。如果当前上下文没有这些参数,那么禁止用户访问这个页面。

<siteMapNode url="Course/Group/GroupDetail.aspx" title="Group Detail" rule="cid;gid">
这是两个函数,递归处理所有的路径。   private string MakeURL(SiteMapNode node)
    {
        node.ReadOnly = false;
        //find the static url
        string url = MySiteMap.FindForward(node.Title);
        if (node["rule"] != null && node["rule"].Length > 0)
        {
            //if have the rule,then check
            string[] paramSet = node["rule"].Split(';');
            //check
            for (int i = 0; i < paramSet.Length; i++)
            {
                //if request have not such a param, then invoke self to check his parent
                if (HttpContext.Current.Request.Params[paramSet[i]] == null)
                    return MakeURL(node.ParentNode);
            }
            //if pass ,then add all the params and return the value
            url += "?";
            for (int i = 0; i < paramSet.Length; i++)
            {
                string key = paramSet[i];
                //'cid'--->'cid=1'. the former format is like : rule='cid;tid'
                url = url + key + "=" + HttpContext.Current.Request.Params[key] + "&";
            }
            return url.Substring(0, url.Length - 1); //remove last '&'

        }
        else
        {
            //if there is no rule then return the url directly
            return url;
        }
    }    private void ReBindData(SiteMapNode root)
    {
        string url = MakeURL(root);
        if (url != "")
            root.Url = url;
        for (int i = 0; i < root.ChildNodes.Count; i++)
        {
            ReBindData(root.ChildNodes[i]);
        }
    }在ReBindData里面递归调用MakeUrl函数。
MakeUrl函数里面调用的MySiteMap.FindForward函数就是来自那位http://quitgame.cnblogs.com/archive/2005/11/24/283910.aspx的实现。
不过应用的是后需要做一些改动:他原来的实现是用静态的类如此加载
        //SiteMapNodeCollection smc = SiteMap.RootNode.GetAllNodes();
        //siteMapCol = new NameValueCollection();

        //IEnumerator ie = smc.GetEnumerator();
        //while (ie.MoveNext())
        //{
        //    siteMapCol[((SiteMapNode)ie.Current).Title] = ((SiteMapNode)ie.Current).Url;
        //}但是,由于用户在没有登陆的时候,限于权限,它能访问的页面有限,所以SiteMap.RootNode.GetAllNodes();得到的不是所有数据,可能只是一部分或者0。
改动方式就是自己写一个函数,直接读取xml文件,递归获取所有数据定义。

出处:BLOG 随心所欲

上一篇:SPAN和DIV的区别 下一篇:Photoshop制作残损的墙壁
本栏目热门文章
·如何实现无刷新的DropdownList联动效果 2005-10-4
·使用HttpWebRequest向网站模拟上传数据 2005-10-4
·当DataSet中包含主/子表时,Update更新步骤 2005-10-6
·ASP.NET2.0实现无刷新客户端回调 2005-11-13
·ASP.NET中文件上传下载方法集合 2006-5-28
·分享个极好的无刷新二级联动下拉列表,同样适用与firefox 2005-10-19
·在Web DataGrid中当鼠标移到某行与离开时行的颜色发生改变( 2005-10-4
·ASP.NET中实现Flash与.NET的紧密集成 2005-11-21
·关于Asp.net页面Page_Load被执行两次的问题 2005-10-4
·ASP.NET极限:页面导航 (翻译) 2005-10-8
新近更新文章
·在VS2003中直接用DREAMWEAVER8打开ASPX文件 2006-6-22
·asp.net2.0中异步调用WebService(异步页) 2006-6-22
·ASP.NET2.0数据库入门之常见错误 2006-6-22
·ASP.NET 2.0中XSLT的使用 2006-6-22
·.Net2.0 使用ConfigurationManager读写配置 2006-6-22
·.net 做的IP 访问限制 2006-6-22
·ASP.Net2.0 GridView 多列排序,显示排序图标,分页 2006-6-22
·数据岛出到Excel最为简易的方法 2006-6-20
·权限树中Checkbox的操作[Asp.Net2.0] 2006-6-20
·使用Data Access Application Block 得到 2006-6-20
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: