Skip to content

Commit

Permalink
Fixes maartenba#213, added IncludeAmbientRequestValues as a feature o…
Browse files Browse the repository at this point in the history
…f ISiteMapNode and provided support for populating this property through XML, Dynamic Nodes, and MvcSiteMapNodeAttribute.
  • Loading branch information
NightOwl888 committed Mar 1, 2014
1 parent 93fbf33 commit 7940dd9
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromProviderNode(ISiteMap siteMap,
}
siteMapNode.PreservedRouteParameters.AddRange(node.GetAttributeValue("preservedRouteParameters"), new[] { ',', ';' });
siteMapNode.UrlResolver = node.GetAttributeValue("urlResolver");
siteMapNode.IncludeAmbientRequestValues = bool.Parse(node.GetAttributeValueOrFallback("includeAmbientRequestValues", "false"));

// Add inherited route values to sitemap node
foreach (var inheritedRouteParameter in node.GetAttributeValue("inheritedRouteParameters").Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromProviderNode(Sy
}
siteMapNode.PreservedRouteParameters.AddRange(node.GetAttributeValue("preservedRouteParameters"), new[] { ',', ';' });
siteMapNode.UrlResolver = node.GetAttributeValue("urlResolver");
siteMapNode.IncludeAmbientRequestValues = bool.Parse(node.GetAttributeValueOrFallback("includeAmbientRequestValues", "false"));

// Add inherited route values to sitemap node
foreach (var inheritedRouteParameter in node.GetAttributeValue("inheritedRouteParameters").Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMa
siteMapNode.RouteValues.AddRange(attribute.Attributes, false);
siteMapNode.PreservedRouteParameters.AddRange(attribute.PreservedRouteParameters, new[] { ',', ';' });
siteMapNode.UrlResolver = attribute.UrlResolver;
siteMapNode.IncludeAmbientRequestValues = attribute.IncludeAmbientRequestValues;

// Specified area, controller and action properties will override any
// provided in the attributes collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromMvcSiteMapNodeA
node.RouteValues.AddRange(attribute.Attributes, false);
node.PreservedRouteParameters.AddRange(attribute.PreservedRouteParameters, new[] { ',', ';' });
node.UrlResolver = attribute.UrlResolver;
node.IncludeAmbientRequestValues = attribute.IncludeAmbientRequestValues;

// Specified area, controller and action properties will override any
// provided in the attributes collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected virtual bool IsKnownAttribute(string attributeName)
|| attributeName == "imageUrl"
|| attributeName == "inheritedRouteParameters"
|| attributeName == "preservedRouteParameters"
|| attributeName == "includeAmbientRequestValues"
|| attributeName == "canonicalUrl"
|| attributeName == "canonicalKey"
|| attributeName == "metaRobotsValues";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ protected virtual ISiteMapNode GetSiteMapNodeFromXmlElement(ISiteMap siteMap, XE
siteMapNode.RouteValues.AddRange(node, false);
siteMapNode.PreservedRouteParameters.AddRange(node.GetAttributeValue("preservedRouteParameters"), new[] { ',', ';' });
siteMapNode.UrlResolver = node.GetAttributeValue("urlResolver");
siteMapNode.IncludeAmbientRequestValues = bool.Parse(node.GetAttributeValueOrFallback("includeAmbientRequestValues", "false"));

// Area and controller may need inheriting from the parent node, so set (or reset) them explicitly
siteMapNode.Area = area;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromXmlElement(XEle
siteMapNode.RouteValues.AddRange(node, false);
siteMapNode.PreservedRouteParameters.AddRange(node.GetAttributeValue("preservedRouteParameters"), new[] { ',', ';' });
siteMapNode.UrlResolver = node.GetAttributeValue("urlResolver");
siteMapNode.IncludeAmbientRequestValues = bool.Parse(node.GetAttributeValueOrFallback("includeAmbientRequestValues", "false"));

// Area and controller may need inheriting from the parent node, so set (or reset) them explicitly
siteMapNode.Area = area;
Expand Down
9 changes: 9 additions & 0 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/DynamicNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ public virtual UpdatePriority UpdatePriority
/// </value>
public virtual bool? Clickable { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to include ambient request values
/// (from the RouteValues and/or query string) when resolving URLs.
/// </summary>
/// <value><b>true</b> to include ambient values (like MVC does); otherwise <b>false</b>.</value>
public virtual bool? IncludeAmbientRequestValues { get; set; }

/// <summary>
/// Copies the values for matching properties on an <see cref="T:MvcSiteMapNodeProvider.ISiteMapNode"/> instance, but
/// doesn't overwrite any values that are not set in this <see cref="T:MvcSiteMapNodeProvider.DynamicNode"/> instance.
Expand Down Expand Up @@ -276,6 +283,8 @@ public virtual void SafeCopyTo(ISiteMapNode node)
node.UrlResolver = this.UrlResolver;
if (this.Clickable != null)
node.Clickable = (bool)this.Clickable;
if (this.IncludeAmbientRequestValues != null)
node.IncludeAmbientRequestValues = (bool)this.IncludeAmbientRequestValues;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ public interface IMvcSiteMapNodeAttribute
/// </value>
string PreservedRouteParameters { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to include ambient request values
/// (from the RouteValues and/or query string) when resolving URLs.
/// </summary>
/// <value><b>true</b> to include ambient values (like MVC does); otherwise <b>false</b>.</value>
bool IncludeAmbientRequestValues { get; set; }

/// <summary>
/// Gets or sets the attributes (optional).
///
Expand Down
1 change: 1 addition & 0 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/ISiteMapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public interface ISiteMapNode
IPreservedRouteParameterCollection PreservedRouteParameters { get; }
RouteData GetRouteData(HttpContextBase httpContext);
bool MatchesRoute(IDictionary<string, object> routeValues);
bool IncludeAmbientRequestValues { get; set; }

string Area { get; set; }
string Controller { get; set; }
Expand Down
15 changes: 15 additions & 0 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/LockableSiteMapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,21 @@ public override string Route
}
}

/// <summary>
/// Gets or sets a value indicating whether to include ambient request values
/// (from the RouteValues and/or query string) when resolving URLs.
/// </summary>
/// <value><b>true</b> to include ambient values (like MVC does); otherwise <b>false</b>.</value>
public override bool IncludeAmbientRequestValues
{
get { return base.IncludeAmbientRequestValues; }
set
{
this.ThrowIfReadOnly("IncludeAmbientRequestValues");
base.IncludeAmbientRequestValues = value;
}
}

#endregion

#region MVC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class MvcSiteMapNodeAttribute : Attribute, IMvcSiteMapNodeAttribute
public MvcSiteMapNodeAttribute()
{
Clickable = true;
IncludeAmbientRequestValues = false;
}

/// <summary>
Expand Down Expand Up @@ -163,6 +164,13 @@ public MvcSiteMapNodeAttribute()
/// </value>
public string PreservedRouteParameters { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to include ambient request values
/// (from the RouteValues and/or query string) when resolving URLs.
/// </summary>
/// <value><b>true</b> to include ambient values (like MVC does); otherwise <b>false</b>.</value>
public bool IncludeAmbientRequestValues { get; set; }

/// <summary>
/// Gets or sets the attributes (optional).
///
Expand Down
10 changes: 9 additions & 1 deletion src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public override string Url
public override void ResolveUrl()
{
if (this.CacheResolvedUrl && string.IsNullOrEmpty(this.UnresolvedUrl) &&
this.preservedRouteParameters.Count == 0 && !this.RouteValues.ContainsCustomKeys)
this.preservedRouteParameters.Count == 0 && !this.IncludeAmbientRequestValues)
{
this.resolvedUrl = this.GetResolvedUrl();
}
Expand Down Expand Up @@ -657,6 +657,13 @@ public override bool MatchesRoute(IDictionary<string, object> routeValues)
return this.RouteValues.MatchesRoute(routeValues);
}

/// <summary>
/// Gets or sets a value indicating whether to include ambient request values
/// (from the RouteValues and/or query string) when resolving URLs.
/// </summary>
/// <value><b>true</b> to include ambient values (like MVC does); otherwise <b>false</b>.</value>
public override bool IncludeAmbientRequestValues { get; set; }

#endregion

#region MVC
Expand Down Expand Up @@ -723,6 +730,7 @@ public override void CopyTo(ISiteMapNode node)
node.Route = this.Route;
this.RouteValues.CopyTo(node.RouteValues);
this.PreservedRouteParameters.CopyTo(node.PreservedRouteParameters);
node.IncludeAmbientRequestValues = this.IncludeAmbientRequestValues;
// NOTE: Area, Controller, and Action are covered under RouteValues.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public virtual bool IsAccessibleToUser()
public abstract IPreservedRouteParameterCollection PreservedRouteParameters { get; }
public abstract RouteData GetRouteData(HttpContextBase httpContext);
public abstract bool MatchesRoute(IDictionary<string, object> routeValues);
public abstract bool IncludeAmbientRequestValues { get; set; }
public abstract string Area { get; set; }
public abstract string Controller { get; set; }
public abstract string Action { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected virtual string ResolveRouteUrl(ISiteMapNode node, string area, string
// which doesn't consume resources
using (var nullWriter = new StreamWriter(Stream.Null))
{
var requestContext = this.CreateRequestContext(node, true, nullWriter);
var requestContext = this.CreateRequestContext(node, nullWriter);
result = this.ResolveRouteUrl(node, area, controller, action, routeValues, requestContext);
}

Expand Down Expand Up @@ -110,9 +110,9 @@ protected virtual HttpContextBase CreateHttpContext(ISiteMapNode node, TextWrite
return this.mvcContextFactory.CreateHttpContext(node, uri, writer);
}

protected virtual RequestContext CreateRequestContext(ISiteMapNode node, bool includeAmbientRequestValues, TextWriter writer)
protected virtual RequestContext CreateRequestContext(ISiteMapNode node, TextWriter writer)
{
if (!includeAmbientRequestValues)
if (!node.IncludeAmbientRequestValues)
{
var httpContext = this.CreateHttpContext(node, writer);
return this.mvcContextFactory.CreateRequestContext(httpContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@
</xs:annotation>
</xs:attribute>

<xs:attribute name="includeAmbientRequestValues" type="xs:boolean" use="optional">
<xs:annotation>
<xs:documentation>
Optional. Whether or not to include request route values and/or query string values when resolving the
URL (for route/action URLs only). Default is false.
</xs:documentation>
</xs:annotation>
</xs:attribute>

<xs:attribute name="canonicalUrl" type="xs:string" use="optional">
<xs:annotation>
<xs:documentation>
Expand Down

0 comments on commit 7940dd9

Please sign in to comment.