2016-07-07 82 views
0

這個問題打擾了我幾個小時。這裏是我的路線。地圖路線:保護路徑參數包含UrlParameter.Optional時的問題

routes.MapRoute(
      "SiteArea", 
      "SiteArea/{area}/{link}", 
      new { controller = "SiteArea", action = "Dispatch", link = UrlParameter.Optional }, 
      namespaces 
     ); 

鏈接是可選的,可以爲null。在Mvc.sitemap:

<mvcSiteMapNode title="SiteArea" controller="SiteArea" action="Dispatch" route="SiteArea" preservedRouteParameters="area,link" /> 

在C#中的mvc行動:

public override ActionResult Dispatch(string area, string link) 
    { 
     var node = SiteMaps.Current.CurrentNode; 
     if (node != null) 
     { 
      node.ParentNode.Title = area; 
      node.Title = link; 
     } 

     return base.Dispatch(area, link); 
    } 

SiteMaps.Current.CurrentNode

總是拋出異常:

The node with key _Home_Index_GET_Home__SiteArea_Dispatch_GET_SiteArea_' and title 'SiteArea' has 'area' configured in both RouteValues and PreservedRouteParameters, which is not allowed. PreservedRouteParameters copies the route value from the current HTTP request which would overwrite your configured RouteValue in every case. Either remove 'area' from PreservedRouteParameters or as a configured RouteValue. 
    Alternatively, if you are configuring the node in XML and intend to use 'area' as a custom attribute, use the 'MvcSiteMapProvider_AttributesToIgnore' configuration setting to ensure 'area' is not automatically added to RouteValues. If using external DI, this setting is injected into the constructor of 'SiteMapXmlReservedAttributeNameProvider'. 

我知道這個例外是假的,不會告訴任何對這個問題有用的東西。 但是,我有完全相同的情況,除了鏈接不UrlParameter.Optional - 不可爲空,它工作正常。 任何人都可以解釋或提供解決此問題 - 當第二個保存參數是可選的?

回答

0

我剛纔發現問題的根本原因。我使用面積關鍵字:

routes.MapRoute(
       "SiteArea", 
       "SiteArea/{area}/{link}", 
       new { controller = "SiteArea", action = "Dispatch", link = UrlParameter.Optional }, 
       namespaces 
      ); 

和Mvc.sitemap:

<mvcSiteMapNode title="SiteArea" controller="SiteArea" action="Dispatch" preservedRouteParameters="area" > 
     <mvcSiteMapNode title="SiteArea" controller="SiteArea" action="Dispatch" preservedRouteParameters="area,link" /> 
     </mvcSiteMapNode> 

改變

區域 站點區域

後210

一切都像一個魅力。

0

areaMvcSiteMapProvider中的保留路由值,以防您的項目使用MVC區域。如果您未使用MVC區域,則仍然有一個自動路由值用作默認區域(area = "")。

因此,您需要將您的路由密鑰更改爲除area以外的其他名稱。否則,由於此衝突,此值不能放入preservedRouteParameters

routes.MapRoute(
     "SiteArea", 
     "SiteArea/{myArea}/{link}", 
     new { controller = "SiteArea", action = "Dispatch", link = UrlParameter.Optional }, 
     namespaces 
    ); 

<mvcSiteMapNode title="SiteArea" controller="SiteArea" action="Dispatch" route="SiteArea" preservedRouteParameters="myArea,link" /> 
+0

準確地說,這是準確和乾淨的解釋,希望沒有更多的人重複這個錯誤,這讓我花了數小時才弄清楚, – Henry