SiteMap的WildCard匹配

 

SiteMap默认是严格匹配路径的,比如URL为test.aspx?id=1的SiteMapNode是不会匹配test.aspx?id=1&pid=2的路径的,这时候需要程序员辅助进行匹配。在Global.asax的Application_Start中注册监听SiteMap.SiteMapResolve事件,在其中根据e.Context.Request进行判断,返回合适的SiteMap,参考:http://stackoverflow.com/questions/291908/using-web-sitemap-with-dynamic-urls-url-routing。例子代码:
        SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)

        {

            if (SiteMap.CurrentNode != null)

            {

                return SiteMap.CurrentNode;

            }

            var Request = e.Context.Request;

            if (Request.AppRelativeCurrentExecutionFilePath.Equals("~/Forum.aspx"))

            {

                //由于分页pid的存在,所以没法精确匹配SiteMapNode的路径,需要辅助定位

                string fid = Request["fid"];

                string forumurl = string.Format("~/Forum.aspx?fid={0}",fid);

                var nodes = SiteMap.RootNode.GetAllNodes().Cast<SiteMapNode>();//转换为linq能够查询的强类型

                //从所有节点中找出url等于论坛首页url的node

                var result = from node in nodes

                             where node.Url == forumurl

                             select node;

                return result.Single();

            }

            else

            {

                return null;

            }

        }

转载自:http://www.rupeng.com/forum/thread-11347-1-1-uid7.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值