2011-06-16 24 views
8

我正在使用來自http://mvcsitemap.codeplex.com/的MvcSiteMapProvider爲我的項目創建麪包屑路徑。我有一些網址需要傳遞給相應用戶的信息,例如http:// localhost:52306/Home/User?ID = 101101MVC站點提供商 - 在麪包屑跟蹤中維護URL參數

當我進一步瀏覽站點地圖(例如http:// localhost:52306/Home/User/Details?ID = 101101)並嘗試使用麪包屑鏈接帶我回到「用戶」頁面,ID參數丟失。我曾嘗試將SiteMapPreserveRouteData屬性添加到操作方法,但它們似乎沒有做任何事情。有沒有簡單的方法來確保這個ID信息被保存?我以爲SiteMapPreserveRouteDataAttribute應該這樣做,所以有什麼我做錯了屬性?我的方法看起來是這樣的:

[SiteMapPreserveRouteData] 
public ActionResult User() 
{ 
    //code 
} 

讓我知道你是否需要我更多的信息。

+0

我有同樣的挑戰...你有沒有找到解決方案? – noocyte 2011-10-25 08:22:53

回答

0

根據MVCSiteMapperProvider上的此線程,您不能使用查詢字符串。它忽略查詢字符串並僅使用路由數據。

Query Strings and MVCSiteMapper

你有沒有想過創造一些新的路線,而不是使用查詢字符串?

1

我這樣做的方式,我把original mvc site map helper source用於渲染breadcrumb,並將其改爲處理參數(儘管在我的項目中我們只顯示參數進行過濾,並允許用戶點擊它們以鬆開其他過濾參數,下面是非常天真的執行節點的文字,只是一個例子可以怎麼做):

private static string SiteMapText(this MvcSiteMapHtmlHelper helper, SiteMapNode node, string linkCssClass, IDictionary<string, object> htmlAttributes) 
    { 
     var extraAttributes = new StringBuilder(); 
     foreach (var attribute in htmlAttributes) 
     { 
      extraAttributes.Append(" " + attribute.Key + "=\"" + attribute.Value + "\""); 
     }    

     string spanHtml; 
     var paramDictionary = helper.HtmlHelper.ViewContext.RequestContext.HttpContext.Request.Params.ToDictionary(); 
     var queryParams = paramDictionary.Select(x => string.Format("{0}:{1}", x.Key, x.Value)); 

     // here you add request parameters 
     var title = helper.HtmlHelper.Encode(string.Format("{0} ({1})", node.Title, string.Join(";", queryParams))); 

     if (!string.IsNullOrEmpty(linkCssClass)) 
     { 
      spanHtml = string.Format("<span><span class=\"{0}\"{1}>{2}</span>", linkCssClass, extraAttributes, title); 
     } 
     else 
     { 
      spanHtml = string.Format("<span><span{1}>{0}</span>", title, extraAttributes); 
     } 

     return spanHtml; 
    } 

在可以調整SiteMapLink方法相同的方式,包括當前節點的請求參數。