2010-01-29 60 views
1

我有类似于这里所描述的一个问题:
ASP.NET URL Routing with WebForms - Using the SiteMap地图和URL路由

我的ASP.Net WebForms的web应用有一个网站地图,与此类似:

<?xml version="1.0" encoding="utf-8" ?> 
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > 
    <siteMapNode title="Root"> 
    <siteMapNode url="Home" title="Home" /> 
     <siteMapNode url="Home?" title="Home" /> 
     <siteMapNode url="Faq" title="FAQ" /> 
    </siteMapNode> 
    <siteMapNode url="Reports" title="Your reports" /> 
     <siteMapNode url="Reports?" title="Your reports" /> 
     <siteMapNode url="ExtraReports" title="Extra reports" /> 
    </siteMapNode> 
    <siteMapNode url="Presentations" title="Your presentations" /> 
     <siteMapNode url="Presentations?" title="Your presentations" /> 
     <siteMapNode url="ExtraPresentations" title="Extra presentations" /> 
    </siteMapNode> 
</siteMapNode> 

我想使用以下格式的网址:
:// host/projects/{company}/{projectno}/Home
:/ /主机/项目/微软/ 10 /主页
://主机/项目/微软/ 11 /报告
://主机/项目/苹果/ 10/ExtraReports

的URL路由的路由/项目/ {公司}/{projectno}/{pagename}改为/Pages/{pagename}.aspx。

我的导航是由顶部一个TabStrip它应该包含在左侧用他们的子项(例如,用家选择应该是:首页,有问必答家,报告和演示和菜单。

我的问题:

  1. 什么是处理重复的SiteMapNode网址的正确方法?

  2. 如何防止TabStrip生成如下URL:// host/Home和:// host/ExtraReports?
    我需要保持目前的公司和projectno选择,并在站点地图忽略相对URL

  3. 标签栏和菜单控制需要认识到所选择的内容,以显示活动的选择。什么是适合所有这一切的解决方案?

回答

2

愿意花几个小时后,我想我已经来到一个好的(足够:))解决方案。

网站地图文件
通知没有网址的节点将指向父

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"> 
    <siteMapNode> 
     <siteMapNode title="Home" url="Home"> 
      <siteMapNode title="Home" /> 
      <siteMapNode title="xxx" url="Cycle"/> 
      <siteMapNode title="yyy" url="Survey" /> 
      <siteMapNode title="zzzteam" url="Team" /> 
     </siteMapNode> 
     ... 

实用工具类来生成网址
CurrentContext是一个静态参考的RequestContext

在母版
导航个

数据绑定事件是一个Telerik的标签栏,子导航是ASP.Net菜单

private void navigation_TabDataBound(object sender, Telerik.Web.UI.RadTabStripEventArgs e) { 
    if (!String.IsNullOrEmpty(e.Tab.NavigateUrl)) {    
      SiteMapNode node = (SiteMapNode) e.Tab.DataItem; 

    // Is this tab a parent of the current navigation node? Then select it 
    if (SiteMap.CurrentNode.IsDescendantOf(node)) e.Tab.Selected = true; 
      e.Tab.NavigateUrl = XRouteHandler.GetPageUrl(node); 
    } 
} 

private void subNavigation_MenuItemDataBound(object sender, MenuEventArgs e) { 
    SiteMapNode itemNode = (SiteMapNode) e.Item.DataItem; 
    SiteMapNode currentNode = SiteMap.CurrentNode; 

    // SiteMapNodes without url will point to the parent node 
    if (String.IsNullOrEmpty(itemNode.Url)) { 
     itemNode = itemNode.ParentNode; 
     e.Item.Selectable = true; 
    } 

    // Is this menu item the node itself or one of it's parents? Then select it 
    if (currentNode == itemNode || currentNode.IsDescendantOf(itemNode)) 
     e.Item.Selected = true; 
     e.Item.NavigateUrl = XRouteHandler.GetPageUrl(itemNode); 
    } 
} 

XmlSiteMapProvider实施
这个解决方案是不够好,现在,稍后会研究它来美化它:)

public override SiteMapNode FindSiteMapNode(HttpContext context) { 
     string pageName = context.Request.Url.Segments.Last().Trim('/'); 
     foreach (SiteMapNode node in SiteMap.RootNode.GetAllNodes()) 
       if (node.Url.EndsWith(pageName, StringComparison.InvariantCultureIgnoreCase)) return node; 
     return SiteMap.RootNode; 
} 
1

Zyphax,

这是最有可能的,你将不得不重新编写tabstrip的和菜单控制考虑您的路线。这也意味着你可以删除重复的SiteMapNodes。

相反,您需要编写一些代码来遍历SiteMap树,以获取最主要的祖先,它是主页的子节点。这是一个可能有所帮助的扩展方法。

public SiteMapNode FurthestAncestor(this SiteMapNode node) 
{ 
    while (node.Key != this.RootNode.Key && node.ParentNode.Key != this.RootNode.Key) 
    { 
     node = node.ParentNode; 
    } 

    return node; 
} 

而且使用相对URL将有助于../Presentations而不是/项目/苹果/ 10 /演示

+0

我今天一直在办公室里为此工作。我想我找到了一个很好的解决方案,它确实与TabStrip和Menu控件有关。我明天会发布。日Thnx。 – Zyphrax 2010-02-01 21:57:14

+0

寻找下面的解决方案.. – Zyphrax 2010-02-02 14:36:48