Skip to content

Commit

Permalink
Added exception for nodes that are configured with the same key in bo…
Browse files Browse the repository at this point in the history
…th PreservedRouteParameters and RouteValues, which is checked when the node is added to the SiteMap.
  • Loading branch information
NightOwl888 committed Aug 31, 2013
1 parent a36d9d9 commit 1ffb80a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,11 @@
<data name="ReflectionSiteMapBuilderRootKeyAmbiguous" xml:space="preserve">
<value>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.</value>
</data>
<data name="SiteMapNodeSameKeyInRouteValueAndPreservedRouteParameter" xml:space="preserve">
<value>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'.</value>
</data>
</root>
18 changes: 18 additions & 0 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public virtual void AddNode(ISiteMapNode node)
/// </exception>
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)
{
Expand Down Expand Up @@ -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

}
Expand Down

0 comments on commit 1ffb80a

Please sign in to comment.