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 隨心所欲
新聞熱點
疑難解答
圖片精選