2010-01-29 59 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