v1.0.0: Route constraint for device slugs
This commit is contained in:
@@ -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<string> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user