Skip to content

Commit

Permalink
Merge branch 'patch-336' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
NightOwl888 committed Jul 25, 2014
2 parents fcedd7b + 5d44271 commit 2d13d54
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
36 changes: 35 additions & 1 deletion src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
Expand Down Expand Up @@ -280,6 +281,13 @@ public virtual bool EnableLocalization
get { return this.siteMapSettings.EnableLocalization; }
}

/// <summary>
/// Retrieves a <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> object that represents the page at the specified URL.
/// </summary>
/// <param name="rawUrl">A URL that identifies the page for which to retrieve a <see cref="T:MvcSiteMapProvider.ISiteMapNode"/>.</param>
/// <returns>A <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> that represents the page identified by rawURL; otherwise, <b>null</b>,
/// if no corresponding <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> is found or if security trimming is enabled and the
/// <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> cannot be returned for the current user.</returns>
public virtual ISiteMapNode FindSiteMapNode(string rawUrl)
{
if (rawUrl == null)
Expand All @@ -296,16 +304,35 @@ public virtual ISiteMapNode FindSiteMapNode(string rawUrl)
// and the current URL will be the absolute URL that is passed.
var publicFacingUrl = this.urlPath.GetPublicFacingUrl(this.HttpContext);
var currentUrl = new Uri(publicFacingUrl, rawUrl);

// Search the internal dictionary for the URL that is registered manually.
var node = this.FindSiteMapNodeFromUrl(currentUrl.PathAndQuery, currentUrl.AbsolutePath, currentUrl.Host, HttpContext.CurrentHandler);

// Search for the URL by creating a context based on the new URL and matching route values.
if (node == null)
{
// Create a TextWriter with null stream as a backing stream
// which doesn't consume resources
using (var nullWriter = new StreamWriter(Stream.Null))
{
// Create a new HTTP context using the current URL.
var currentUrlHttpContext = this.mvcContextFactory.CreateHttpContext(null, currentUrl, nullWriter);

// Find node for the passed-in URL using the new HTTP context. This will do a
// match based on route values and/or query string values.
node = this.FindSiteMapNodeFromMvc(currentUrlHttpContext);
}
}

return this.ReturnNodeIfAccessible(node);
}

/// <summary>
/// Retrieves a <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> object that represents the currently requested page using the current <see cref="T:System.Web.HttpContext"/> object.
/// </summary>
/// <returns>
/// A <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> that represents the currently requested page; otherwise, null, if no corresponding <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> can be found in the <see cref="T:MvcSiteMapProvider.SiteMapNode"/> or if the page context is null.
/// A <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> that represents the currently requested page; otherwise, <b>null</b>,
/// if no corresponding <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> can be found in the <see cref="T:MvcSiteMapProvider.SiteMapNode"/> or if the page context is null.
/// </returns>
public virtual ISiteMapNode FindSiteMapNodeFromCurrentContext()
{
Expand All @@ -322,6 +349,13 @@ public virtual ISiteMapNode FindSiteMapNode(ControllerContext context)
return this.FindSiteMapNode(context.HttpContext);
}

/// <summary>
/// Retrieves a <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> object based on a specified key.
/// </summary>
/// <param name="key">A lookup key with which a <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> instance is created.</param>
/// <returns>A <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> that represents the page identified by key; otherwise, <b>null</b>,
/// if no corresponding <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> is found or if security trimming is enabled and the
/// <see cref="T:MvcSiteMapProvider.ISiteMapNode"/> cannot be returned for the current user. The default is null.</returns>
public virtual ISiteMapNode FindSiteMapNodeFromKey(string key)
{
ISiteMapNode node = null;
Expand Down
46 changes: 34 additions & 12 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Html/MenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, bool startFro
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, bool startFromCurrentNode, bool startingNodeInChildLevel, bool showStartingNode, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = startFromCurrentNode ? GetCurrentNode(helper.SiteMap) : helper.SiteMap.RootNode;
ISiteMapNode startingNode = startFromCurrentNode ? GetCurrentNode(helper.SiteMap, true) : helper.SiteMap.RootNode;
return Menu(helper, startingNode, startingNodeInChildLevel, showStartingNode, Int32.MaxValue, false, sourceMetadata);
}

Expand Down Expand Up @@ -204,7 +204,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, bool startFro
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, bool startFromCurrentNode, bool startingNodeInChildLevel, bool showStartingNode, bool visibilityAffectsDescendants, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = startFromCurrentNode ? GetCurrentNode(helper.SiteMap) : helper.SiteMap.RootNode;
ISiteMapNode startingNode = startFromCurrentNode ? GetCurrentNode(helper.SiteMap, true) : helper.SiteMap.RootNode;
return Menu(helper, null, startingNode, startingNodeInChildLevel, showStartingNode, Int32.MaxValue, false, visibilityAffectsDescendants, sourceMetadata);
}

Expand Down Expand Up @@ -249,7 +249,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, int startingN
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, int startingNodeLevel, int maxDepth, bool allowForwardSearch, bool drillDownToCurrent, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap), startingNodeLevel, allowForwardSearch);
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap, true), startingNodeLevel, allowForwardSearch);
if (startingNode == null)
{
return MvcHtmlString.Empty;
Expand Down Expand Up @@ -301,7 +301,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, int startingN
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, int startingNodeLevel, int maxDepth, bool allowForwardSearch, bool drillDownToCurrent, bool visibilityAffectsDescendants, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap), startingNodeLevel, allowForwardSearch);
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap, true), startingNodeLevel, allowForwardSearch);
if (startingNode == null)
{
return MvcHtmlString.Empty;
Expand Down Expand Up @@ -435,7 +435,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, int startingN
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, int startingNodeLevel, bool startingNodeInChildLevel, bool showStartingNode, int maxDepth, bool allowForwardSearch, bool drillDownToCurrent, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap), startingNodeLevel, allowForwardSearch);
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap, true), startingNodeLevel, allowForwardSearch);
if (startingNode == null)
{
return MvcHtmlString.Empty;
Expand Down Expand Up @@ -490,7 +490,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, int startingN
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, int startingNodeLevel, bool startingNodeInChildLevel, bool showStartingNode, int maxDepth, bool allowForwardSearch, bool drillDownToCurrent, bool visibilityAffectsDescenants, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap), startingNodeLevel, allowForwardSearch);
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap, true), startingNodeLevel, allowForwardSearch);
if (startingNode == null)
{
return MvcHtmlString.Empty;
Expand Down Expand Up @@ -791,7 +791,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templa
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templateName, bool startFromCurrentNode, bool startingNodeInChildLevel, bool showStartingNode, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = startFromCurrentNode ? GetCurrentNode(helper.SiteMap) : helper.SiteMap.RootNode;
ISiteMapNode startingNode = startFromCurrentNode ? GetCurrentNode(helper.SiteMap, true) : helper.SiteMap.RootNode;
return Menu(helper, templateName, startingNode, startingNodeInChildLevel, showStartingNode, Int32.MaxValue, false, sourceMetadata);
}

Expand Down Expand Up @@ -839,7 +839,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templa
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templateName, bool startFromCurrentNode, bool startingNodeInChildLevel, bool showStartingNode, bool visibilityAffectsDescendants, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = startFromCurrentNode ? GetCurrentNode(helper.SiteMap) : helper.SiteMap.RootNode;
ISiteMapNode startingNode = startFromCurrentNode ? GetCurrentNode(helper.SiteMap, true) : helper.SiteMap.RootNode;
return Menu(helper, templateName, startingNode, startingNodeInChildLevel, showStartingNode, Int32.MaxValue, false, visibilityAffectsDescendants, sourceMetadata);
}

Expand Down Expand Up @@ -887,7 +887,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templa
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templateName, int startingNodeLevel, int maxDepth, bool allowForwardSearch, bool drillDownToCurrent, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap), startingNodeLevel, allowForwardSearch);
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap, true), startingNodeLevel, allowForwardSearch);
if (startingNode == null)
{
return MvcHtmlString.Empty;
Expand Down Expand Up @@ -986,7 +986,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templa
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templateName, int startingNodeLevel, bool startingNodeInChildLevel, bool showStartingNode, int maxDepth, bool allowForwardSearch, bool drillDownToCurrent, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap), startingNodeLevel, allowForwardSearch);
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap, true), startingNodeLevel, allowForwardSearch);
if (startingNode == null)
{
return MvcHtmlString.Empty;
Expand Down Expand Up @@ -1047,7 +1047,7 @@ public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templa
/// <returns>Html markup</returns>
public static MvcHtmlString Menu(this MvcSiteMapHtmlHelper helper, string templateName, int startingNodeLevel, bool startingNodeInChildLevel, bool showStartingNode, int maxDepth, bool allowForwardSearch, bool drillDownToCurrent, bool visibilityAffectsDescendants, SourceMetadataDictionary sourceMetadata)
{
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap), startingNodeLevel, allowForwardSearch);
ISiteMapNode startingNode = GetStartingNode(GetCurrentNode(helper.SiteMap, true), startingNodeLevel, allowForwardSearch);
if (startingNode == null)
{
return MvcHtmlString.Empty;
Expand Down Expand Up @@ -1332,6 +1332,20 @@ private static MenuHelperModel BuildModel(MvcSiteMapHtmlHelper helper, SourceMet
/// <param name="selectedSiteMapProvider">the current MVC Site Map Provider</param>
/// <returns></returns>
public static ISiteMapNode GetCurrentNode(ISiteMap selectedSiteMap)
{
return GetCurrentNode(selectedSiteMap, false);
}

/// <summary>
/// This determines the deepest node matching the current HTTP context, so if the current URL describes a location
/// deeper than the site map designates, it will determine the closest parent to the current URL and return that
/// as the current node. This allows menu relevance when navigating deeper than the sitemap structure designates, such
/// as when navigating to MVC actions, which are not shown in the menus
/// </summary>
/// <param name="selectedSiteMapProvider">the current MVC Site Map Provider</param>
/// <param name="returnRootNodeIfNotFound">whether to return the root node if the current node is null</param>
/// <returns></returns>
public static ISiteMapNode GetCurrentNode(ISiteMap selectedSiteMap, bool returnRootNodeIfNotFound)
{
// get the node matching the current URL location
var currentNode = selectedSiteMap.CurrentNode;
Expand All @@ -1341,11 +1355,12 @@ public static ISiteMapNode GetCurrentNode(ISiteMap selectedSiteMap)
if (currentNode == null)
{
var url = HttpContext.Current.Request.Url.LocalPath;
var queryString = HttpContext.Current.Request.Url.Query;

while (url.Length > 0)
{
// see if we can find a matching node
currentNode = selectedSiteMap.FindSiteMapNode(url);
currentNode = selectedSiteMap.FindSiteMapNode(url + queryString);

// if we get a hit, stop
if (currentNode != null) break;
Expand All @@ -1357,6 +1372,13 @@ public static ISiteMapNode GetCurrentNode(ISiteMap selectedSiteMap)
}
}

// If the current node is still null, return the root node.
// This is the same way the SiteMap.FindSiteMapNode(rawUrl) method worked in v3.
if (currentNode == null && returnRootNodeIfNotFound)
{
currentNode = selectedSiteMap.RootNode;
}

return currentNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ protected virtual HttpContextBase CreateHttpContext(ISiteMapNode node)

public virtual HttpContextBase CreateHttpContext(ISiteMapNode node, Uri uri, TextWriter writer)
{
if (node == null)
throw new ArgumentNullException("node");
if (uri == null)
throw new ArgumentNullException("uri");
if (writer == null)
Expand Down

0 comments on commit 2d13d54

Please sign in to comment.