SiteMap,網(wǎng)站地圖,在網(wǎng)站建設的時候是很有用的。它可以直接綁定在Men和TreeView控件上,還有一個指示當前路徑的SiteMapPath控件,也可以直接綁定。
這是他常用的xml定義:
<siteMapNode url="Course/Group/GroupList.aspx" title="GroupAdmin" >
這個SiteMap的權限已經(jīng)和Membership結(jié)合起來了,不同權限的用戶所看到的地圖已經(jīng)被控制了。可以配置role屬性來擴展例外的訪問許可。注意,是例外的訪問許可。
<siteMapNode url="Course/Tests/TestList.aspx" title="TestAdmin" role="student">這里有些介紹:http://zmsx.VEVb.com/archive/2006/01/03/310381.aspx
簡單的使用這里不作贅述,只是討論一下怎么和擴展一下,讓他可以訪問資源時附帶參數(shù)。
首先介紹這樣一個資源:MySiteMapTool:http://quitgame.VEVb.com/archive/2005/11/24/283910.aspx
這位仁兄已經(jīng)提供了一個工具,可以在程序中轉(zhuǎn)發(fā)帶參數(shù)的請求
比如: MySiteMap.Forward("Details", "AlbumID={0}&Page={1}", 1, 4);
確是簡單實用。
現(xiàn)在想要的功能是:因為各個液面都需要不同的參數(shù),所以在沒有這些參數(shù)的情況下就禁止用戶訪問那個頁面,轉(zhuǎn)而訪問父一級頁面,遞歸。
首先,SiteMap本身有個SiteMaPResolve事件,在當前路徑被解析時觸發(fā),這是一段來自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;
}
這段代碼只是給當前路徑加載參數(shù)。
曾經(jīng)嘗試過使用類似的方法,但是SiteMapPath搞定了,Menu就綁定不上數(shù)據(jù)了。并且只能處理一部分數(shù)據(jù)。
后來,結(jié)合SiteMapTool那個類,又寫出幾個函數(shù)可以解決這個問題
這是修改之后的sitemap文件,加了一個配置項:rule,里面的參數(shù)是這個頁面需要的參數(shù)。如果當前上下文沒有這些參數(shù),那么禁止用戶訪問這個頁面。
<siteMapNode url="Course/Group/GroupDetail.aspx" title="Group Detail" rule="cid;gid">
這是兩個函數(shù),遞歸處理所有的路徑。 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里面遞歸調(diào)用MakeUrl函數(shù)。
MakeUrl函數(shù)里面調(diào)用的MySiteMap.FindForward函數(shù)就是來自那位http://quitgame.VEVb.com/archive/2005/11/24/283910.aspx的實現(xiàn)。
不過應用的是后需要做一些改動:他原來的實現(xiàn)是用靜態(tài)的類如此加載
//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();得到的不是所有數(shù)據(jù),可能只是一部分或者0。
改動方式就是自己寫一個函數(shù),直接讀取xml文件,遞歸獲取所有數(shù)據(jù)定義。
出處:BLOG 隨心所欲
新聞熱點
疑難解答