這個問題打擾了我幾個小時。這裏是我的路線。地圖路線:保護路徑參數包含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 - 不可爲空,它工作正常。 任何人都可以解釋或提供解決此問題 - 當第二個保存參數是可選的?
準確地說,這是準確和乾淨的解釋,希望沒有更多的人重複這個錯誤,這讓我花了數小時才弄清楚, – Henry