Skip to content

Commit 638a07f

Browse files
committed
More robust route matching for TreeNode<MenuItem>
1 parent 539da63 commit 638a07f

File tree

8 files changed

+77
-20
lines changed

8 files changed

+77
-20
lines changed

src/Libraries/SmartStore.Core/Extensions/HttpExtensions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,6 @@ public static T GetOrAdd<T>(this Cache cache, string key, Func<T> acquirer, Time
171171

172172
public static T GetItem<T>(this HttpContext httpContext, string key, Func<T> factory = null, bool forceCreation = true)
173173
{
174-
if (httpContext?.Items == null)
175-
{
176-
return default(T);
177-
}
178-
179174
return GetItem<T>(new HttpContextWrapper(httpContext), key, factory, forceCreation);
180175
}
181176

src/Libraries/SmartStore.Core/Extensions/RouteExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static string GetAreaName(this RouteData routeData)
1414
{
1515
return (area as string);
1616
}
17+
1718
return routeData.Route.GetAreaName();
1819
}
1920

@@ -24,11 +25,13 @@ public static string GetAreaName(this RouteBase route)
2425
{
2526
return area.Area;
2627
}
28+
2729
var route2 = route as Route;
2830
if ((route2 != null) && (route2.DataTokens != null))
2931
{
3032
return (route2.DataTokens["area"] as string);
3133
}
34+
3235
return null;
3336
}
3437

src/Presentation/SmartStore.Web.Framework/Seo/GenericPathRoute.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public override RouteData GetRouteData(HttpContextBase httpContext)
7777
var urlRecord = urlRecordService.GetBySlug(slug);
7878
if (urlRecord == null)
7979
{
80-
//no URL record found
80+
// no URL record found
8181
data.Values["controller"] = "Error";
8282
data.Values["action"] = "NotFound";
8383
return data;
@@ -105,7 +105,8 @@ public override RouteData GetRouteData(HttpContextBase httpContext)
105105
}
106106
}
107107

108-
// process URL
108+
// process URL
109+
data.DataTokens["UrlRecord"] = urlRecord;
109110
data.Values["SeName"] = urlRecord.Slug;
110111
switch (urlRecord.EntityName.ToLowerInvariant())
111112
{

src/Presentation/SmartStore.Web.Framework/UI/Extensions/NavigatableExtensions.cs

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,81 @@
66
using System.Web.Mvc;
77
using System.Web;
88
using SmartStore.Collections;
9+
using SmartStore.Web.Framework.Seo;
10+
using SmartStore.Core.Domain.Seo;
11+
using SmartStore.Services.Localization;
912

1013
namespace SmartStore.Web.Framework.UI
1114
{
1215
public static class NavigatableExtensions
1316
{
14-
public static bool HasValue(this INavigatable navigatable)
17+
class PathInfo
18+
{
19+
public string RequestPath { get; set; }
20+
public string RoutePath { get; set; }
21+
}
22+
23+
public static bool HasValue(this INavigatable navigatable)
1524
{
1625
return (((navigatable.ActionName.HasValue() && navigatable.ControllerName.HasValue()) || navigatable.ActionName.HasValue() || navigatable.RouteName.HasValue()) || navigatable.Url.HasValue());
1726
}
1827

1928
public static bool IsCurrent(this INavigatable navigatable, ControllerContext controllerContext)
2029
{
21-
string pathAndQuery = controllerContext.HttpContext.Request.Url.PathAndQuery;
22-
string url = navigatable.GenerateUrl(controllerContext.RequestContext);
23-
string comparing = new UrlHelper(controllerContext.RequestContext).RouteUrl(controllerContext.RequestContext.RouteData.Values);
24-
return (url.IsCaseInsensitiveEqual(pathAndQuery) || url.IsCaseInsensitiveEqual(comparing));
25-
}
30+
var url = navigatable.GenerateUrl(controllerContext.RequestContext);
31+
var compare = GetCurrentPathInfo(controllerContext);
32+
33+
if (url.IsCaseInsensitiveEqual(compare.RequestPath))
34+
{
35+
return true;
36+
}
37+
38+
if (compare.RoutePath.HasValue() && url.IsCaseInsensitiveEqual(compare.RoutePath))
39+
{
40+
return true;
41+
}
42+
43+
return false;
44+
}
45+
46+
private static PathInfo GetCurrentPathInfo(ControllerContext controllerContext)
47+
{
48+
var cacheKey = "CurrentPathInfoForSitemapMatching";
49+
50+
return controllerContext.HttpContext.GetItem<PathInfo>(cacheKey, () =>
51+
{
52+
var requestContext = controllerContext.RequestContext;
53+
var request = controllerContext.HttpContext.Request;
54+
var info = new PathInfo
55+
{
56+
RequestPath = request.RawUrl
57+
};
58+
59+
// Path generated by Routing
60+
var routeValues = requestContext.RouteData.Values;
61+
if (requestContext.RouteData.Route is GenericPathRoute)
62+
{
63+
// We pushed matching UrlRecord instance to DataTokens in GenericPathRoute class
64+
var urlRecord = requestContext.RouteData.DataTokens["UrlRecord"] as UrlRecord;
65+
if (urlRecord != null)
66+
{
67+
var routeName = urlRecord.EntityName;
68+
69+
// Also SeName has been pushed to current RouteValues in GenericPathRoute class
70+
if (routeValues.ContainsKey("SeName"))
71+
{
72+
info.RoutePath = new UrlHelper(requestContext).RouteUrl(routeName, new { SeName = routeValues["SeName"] });
73+
}
74+
}
75+
}
76+
else
77+
{
78+
info.RoutePath = new UrlHelper(requestContext).RouteUrl(routeValues);
79+
}
80+
81+
return info;
82+
});
83+
}
2684

2785
public static void Action(this INavigatable navigatable, RouteValueDictionary routeValues)
2886
{
@@ -154,7 +212,7 @@ public static string GenerateUrl(this INavigatable navigatable, UrlHelper urlHel
154212
routeValues[param.Name] = param.Value;
155213

156214
var requestContext = urlHelper.RequestContext;
157-
if (requestContext.RouteData != null && requestContext.RouteData.Route != null)
215+
if (requestContext.RouteData?.Route != null)
158216
{
159217
var virtualPath = requestContext.RouteData.Route.GetVirtualPath(requestContext, routeValues);
160218
if (virtualPath != null)

src/Presentation/SmartStore.Web/Administration/Controllers/TopicController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public ActionResult List(GridCommand command, TopicListModel model)
180180
if (model.WidgetZone.HasValue())
181181
q2 = q2.Where(x => x.WidgetZone.Contains(model.WidgetZone));
182182

183-
return q2;
183+
return q2.OrderBy(x => x.SystemName);
184184
});
185185

186186
gridModel.Data = topics.Select(x =>

src/Presentation/SmartStore.Web/Administration/Scripts/admin.globalinit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var _commonPluginFactories = [
66
// panel toggling
77
function (ctx) {
8-
ctx.find('input[type=checkbox][data-toggler-for]').each(function (i, el) {
8+
ctx.find('input[type=checkbox][data-toggler-for]').each(function (i, el) {
99
SmartStore.Admin.togglePanel(el, false);
1010
});
1111
},

src/Presentation/SmartStore.Web/Administration/Views/Topic/_CreateOrUpdate.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
@helper TabInfo()
2525
{
26-
<table class="adminContent">
26+
<table class="adminContent mb-3">
2727
<tr>
2828
<td class="adminTitle">
2929
@Html.SmartLabelFor(model => model.SystemName)
@@ -101,7 +101,7 @@
101101
@Html.SmartLabelFor(model => model.RenderAsWidget)
102102
</td>
103103
<td class="adminData">
104-
@Html.CheckBoxFor(model => model.RenderAsWidget, new { data_toggler_for = "#pnlRenderAsWidget, #pnlWidgetWrapContent" })
104+
@Html.CheckBoxFor(model => model.RenderAsWidget, new { data_toggler_for = "#pnlRenderAsWidget" })
105105
@Html.ValidationMessageFor(model => model.RenderAsWidget)
106106
</td>
107107
</tr>
@@ -125,7 +125,7 @@
125125
</td>
126126
</tr>
127127
</tbody>
128-
<tbody id="pnlWidgetWrapContent hide">
128+
<tbody id="pnlWidgetWrapContent">
129129
<tr>
130130
<td class="adminTitle">
131131
@Html.SmartLabelFor(model => model.WidgetShowTitle)

src/Presentation/SmartStore.Web/Infrastructure/Routes/3_GenericRoutes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public partial class SeoRoutes : IRouteProvider
1010
{
1111
public void RegisterRoutes(RouteCollection routes)
1212
{
13-
//generic URLs
13+
// Generic URLs
1414
routes.MapGenericPathRoute("GenericUrl",
1515
"{*generic_se_name}",
1616
new { controller = "Common", action = "GenericUrl" },

0 commit comments

Comments
 (0)