diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Resources/Messages.Designer.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Resources/Messages.Designer.cs index 80cfc8aa..5aca843c 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Resources/Messages.Designer.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Resources/Messages.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.18047 +// Runtime Version:4.0.30319.18052 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -303,6 +303,19 @@ internal static string SiteMapNodeReadOnly { } } + /// + /// Looks up a localized string similar to The node with key '{0}' has '{1}' configured in both RouteValues and PreservedRouteParameters, which is not allowed. + /// + ///PreservedRouteParameters always copies the route value from the current HTTP request which would overwrite your configured RouteValue in every case. Either remove '{1}' from PreservedRouteParameters or as a configured RouteValue. + /// + ///Alternatively, if you are configuring the node in XML and intend to use '{1}' as a custom attribute, use the 'MvcSiteMapProvider_AttributesToIgnore' configura [rest of string was truncated]";. + /// + internal static string SiteMapNodeSameKeyInRouteValueAndPreservedRouteParameter { + get { + return ResourceManager.GetString("SiteMapNodeSameKeyInRouteValueAndPreservedRouteParameter", resourceCulture); + } + } + /// /// Looks up a localized string similar to SiteMap is readonly, nodes cannot be modified.. /// diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Resources/Messages.resx b/src/MvcSiteMapProvider/MvcSiteMapProvider/Resources/Messages.resx index cfd970fa..505256f6 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Resources/Messages.resx +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Resources/Messages.resx @@ -216,4 +216,11 @@ There is more than one MvcSiteMapNodeAttribute declared without a parent key. The ParentKey property must be set for all (or all but 1) MvcSiteMapNodeAttribute in the application. + + The node with key '{0}' has '{1}' configured in both RouteValues and PreservedRouteParameters, which is not allowed. + +PreservedRouteParameters always copies the route value from the current HTTP request which would overwrite your configured RouteValue in every case. Either remove '{1}' from PreservedRouteParameters or as a configured RouteValue. + +Alternatively, if you are configuring the node in XML and intend to use '{1}' as a custom attribute, use the 'MvcSiteMapProvider_AttributesToIgnore' configuration setting to ensure '{1}' is not automatically added to RouteValues. If using external DI, this setting is injected into the constructor of 'SiteMapXmlReservedAttributeNameProvider'. + \ No newline at end of file diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs index 2eb87e71..b4188332 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs @@ -102,6 +102,12 @@ public virtual void AddNode(ISiteMapNode node) /// public virtual void AddNode(ISiteMapNode node, ISiteMapNode parentNode) { + if (node == null) + { + throw new ArgumentNullException("node"); + } + ThrowIfSiteMapNodeRoutingConfigInvalid(node); + // Avoid issue with url table not clearing correctly. if (this.FindSiteMapNode(node.Url) != null) { @@ -763,6 +769,18 @@ protected virtual ISiteMapNode ReturnNodeIfAccessible(ISiteMapNode node) return null; } + protected virtual void ThrowIfSiteMapNodeRoutingConfigInvalid(ISiteMapNode node) + { + if (node.PreservedRouteParameters.Count > 0) + { + foreach (var key in node.PreservedRouteParameters) + { + if (node.RouteValues.ContainsKey(key)) + throw new MvcSiteMapException(String.Format(Resources.Messages.SiteMapNodeSameKeyInRouteValueAndPreservedRouteParameter, node.Key, key)); + } + } + } + #endregion }