Fixing matching logic when looking for a menu item to select.

Items with route values (eg. ContentMenuItem) were omitted and not selected when viewing their child pages, non-existent in the menu.
Eg. item pointing to a blog was not selected when one entered one of the blog posts (provided the blog post was not on a menu).
This commit is contained in:
Piotr Szmyd
2014-07-22 21:52:32 +02:00
parent 0d03ad2ca6
commit d08aa1883e

View File

@@ -91,22 +91,21 @@ namespace Orchard.UI.Navigation {
return selectedPath;
}
bool match = false;
// if the menu item doesn't have route values, compare urls
if (currentRequest != null && menuItem.RouteValues == null) {
// compare route values (if any) first
bool match = menuItem.RouteValues != null && RouteMatches(menuItem.RouteValues, currentRouteData);
// if route match failed, try comparing URL strings
if (currentRequest != null && !match) {
string appPath = currentRequest.ApplicationPath ?? "/";
string requestUrl = currentRequest.Path.StartsWith(appPath) ? currentRequest.Path.Substring(appPath.Length) : currentRequest.Path;
string modelUrl = menuItem.Href.Replace("~/", appPath);
modelUrl = modelUrl.StartsWith(appPath) ? modelUrl.Substring(appPath.Length) : modelUrl;
string requestUrl = currentRequest.Path.Replace(currentRequest.ApplicationPath ?? "/", string.Empty);
string modelUrl = menuItem.Href.Replace("~/", currentRequest.ApplicationPath);
modelUrl = modelUrl.Replace(currentRequest.ApplicationPath ?? "/", string.Empty);
if (requestUrl.Equals(modelUrl, StringComparison.OrdinalIgnoreCase) || (!string.IsNullOrEmpty(modelUrl) && requestUrl.StartsWith(modelUrl + "/", StringComparison.OrdinalIgnoreCase))) {
match = true;
}
}
else {
if (RouteMatches(menuItem.RouteValues, currentRouteData)) {
match = true;
}
}
if (match) {
menuItem.Selected = true;