From 2d426b5b07d4d539d93c2ace8a5b82cef33c53fd Mon Sep 17 00:00:00 2001 From: jessikitty Date: Wed, 20 May 2026 15:13:43 +1000 Subject: [PATCH] v1.0.0: Route constraint for device slugs --- Routing/DeviceSlugConstraint.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Routing/DeviceSlugConstraint.cs diff --git a/Routing/DeviceSlugConstraint.cs b/Routing/DeviceSlugConstraint.cs new file mode 100644 index 0000000..b9f64b1 --- /dev/null +++ b/Routing/DeviceSlugConstraint.cs @@ -0,0 +1,28 @@ +using NoticeBoard.Data; +using Microsoft.EntityFrameworkCore; + +namespace NoticeBoard.Routing; + +public class DeviceSlugConstraint : IRouteConstraint +{ + // Reserved paths that should NOT be treated as device slugs + private static readonly HashSet ReservedPaths = new(StringComparer.OrdinalIgnoreCase) + { + "admin", "api", "d", "error", "css", "js", "lib", "uploads", "favicon.ico" + }; + + public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, + RouteValueDictionary values, RouteDirection routeDirection) + { + if (values.TryGetValue(routeKey, out var value) && value is string slug) + { + // Block reserved paths + if (ReservedPaths.Contains(slug)) + return false; + + // Only match lowercase alphanumeric + hyphens + return slug.All(c => char.IsLetterOrDigit(c) || c == '-'); + } + return false; + } +}