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。例子代码: { 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; } } |
SiteMap的WildCard匹配
最新推荐文章于 2024-07-25 22:44:17 发布