Skip to content

Commit 99d08cc

Browse files
committed
Set route params in aikido context
1 parent d92200f commit 99d08cc

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Aikido.Zen.DotNetCore/Middleware/ContextMiddleware.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Concurrent;
2+
using System.Globalization;
23
using System.Runtime.CompilerServices;
34
using Aikido.Zen.Core;
45
using Aikido.Zen.Core.Helpers;
@@ -130,6 +131,7 @@ private bool TryPrepareContext(HttpContext httpContext, out ConcurrentDictionary
130131
UserAgent = httpContext.Request.Headers.TryGetValue("User-Agent", out var userAgent) ? userAgent.FirstOrDefault() ?? string.Empty : string.Empty,
131132
Source = Environment.Version.Major >= 5 ? "DotNetCore" : "DotNetFramework",
132133
Route = GetParametrizedRoute(httpContext),
134+
RouteParams = FlattenRouteParameters(httpContext.GetRouteData()?.Values),
133135
User = httpContext.Items["Aikido.Zen.CurrentUser"] as User
134136
};
135137
return true;
@@ -145,6 +147,29 @@ private bool TryPrepareContext(HttpContext httpContext, out ConcurrentDictionary
145147

146148
}
147149

150+
private static IDictionary<string, string> FlattenRouteParameters(RouteValueDictionary routeValues)
151+
{
152+
var result = new Dictionary<string, string>();
153+
154+
foreach (var kvp in routeValues)
155+
{
156+
if (kvp.Value == null)
157+
{
158+
continue;
159+
}
160+
161+
var value = Convert.ToString(kvp.Value, CultureInfo.InvariantCulture);
162+
if (value == null)
163+
{
164+
continue;
165+
}
166+
167+
result[kvp.Key] = value;
168+
}
169+
170+
return result;
171+
}
172+
148173
/// <summary>
149174
/// Flattens query parameters into individual dictionary entries with indexing for multiple values.
150175
/// </summary>

Aikido.Zen.DotNetFramework/HttpModules/ContextModule.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Runtime.CompilerServices;
56
using System.Threading.Tasks;
@@ -82,6 +83,7 @@ private async Task Context_BeginRequest(object sender, EventArgs e)
8283
UserAgent = httpContext.Request.UserAgent,
8384
Source = "DotNetFramework",
8485
Route = GetParametrizedRoute(httpContext),
86+
RouteParams = FlattenRouteParameters(httpContext.Request?.RequestContext?.RouteData?.Values),
8587
};
8688

8789
Agent.Instance.SetContextMiddlewareInstalled(true);
@@ -218,6 +220,34 @@ private static IDictionary<string, string> FlattenHeaders(System.Collections.Spe
218220
return result;
219221
}
220222

223+
private static IDictionary<string, string> FlattenRouteParameters(RouteValueDictionary routeValues)
224+
{
225+
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
226+
227+
if (routeValues == null)
228+
{
229+
return result;
230+
}
231+
232+
foreach (var kvp in routeValues)
233+
{
234+
if (kvp.Value == null)
235+
{
236+
continue;
237+
}
238+
239+
var value = Convert.ToString(kvp.Value, CultureInfo.InvariantCulture);
240+
if (value == null)
241+
{
242+
continue;
243+
}
244+
245+
result[kvp.Key] = value;
246+
}
247+
248+
return result;
249+
}
250+
221251
/// <summary>
222252
/// Gets a parameterized route from the HTTP context, matching against the route collection
223253
/// and applying route parameter detection when needed.

0 commit comments

Comments
 (0)