使用asp.net2.0中的SiteMap中的一些问题

来源:互联网 发布:淘宝店铺免费推广软件 编辑:程序博客网 时间:2024/04/28 21:57
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
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 随心所欲

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击