国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

使用asp.net2.0中的SiteMap中的一些問題

2024-07-10 13:11:32
字體:
來源:轉載
供稿:網友
,歡迎訪問網頁設計愛好者web開發。

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 隨心所欲

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延安市| 绵竹市| 英德市| 广州市| 石首市| 陈巴尔虎旗| 乳山市| 临沭县| 清镇市| 茂名市| 莱芜市| 扬州市| 白城市| 永善县| 荔波县| 桃园市| 永泰县| 信丰县| 中阳县| 岢岚县| 台北县| 应城市| 河曲县| 兴宁市| 彝良县| 曲松县| 曲靖市| 全南县| 汝州市| 扶余县| 峨山| 方城县| 桃江县| 靖西县| 双桥区| 邯郸县| 康乐县| 花莲县| 当涂县| 利川市| 马公市|