From d7b59050ecc39448c34741552fb489f88457e346 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 14 Jan 2026 18:46:10 -0600 Subject: rearrange model binder --- .../Common/Middleware/UrlEncodingModelBinder.cs | 23 +++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'src/slskd/Common/Middleware/UrlEncodingModelBinder.cs') diff --git a/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs b/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs index 6729b374..27ea3258 100644 --- a/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs +++ b/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs @@ -63,8 +63,18 @@ public class UrlEncodingModelBinder : IModelBinder */ try { + var request = bindingContext.HttpContext.Request; + var model = $"{{{bindingContext.ModelName}}}"; var template = bindingContext.ActionContext.ActionDescriptor.AttributeRouteInfo?.Template ?? string.Empty; + + // if the application is running with a base path (e.g., /slskd), we need to add it to the template + // as it isn't included out of the box + if (request.PathBase.HasValue) + { + template = $"{request.PathBase.Value.Trim('/')}/{template}"; + } + var index = template .Split('/', StringSplitOptions.RemoveEmptyEntries) .Select((segment, index) => new { Segment = segment, Index = index }) @@ -75,18 +85,7 @@ public class UrlEncodingModelBinder : IModelBinder // a value that's already had one pass of url decoding applied var rawUrl = bindingContext.HttpContext.Features.Get()?.RawTarget ?? string.Empty; - var request = bindingContext.HttpContext.Request; - var absolutePath = new Uri($"{request.Scheme}://{request.Host}{rawUrl}").AbsolutePath; // discard query string - - // if the application is running with a base path (e.g., /slskd), we need to remove it - // from the absolute path before splitting, as the route template doesn't include it - var pathBase = request.PathBase.Value ?? string.Empty; - if (!string.IsNullOrEmpty(pathBase) && absolutePath.StartsWith(pathBase, StringComparison.OrdinalIgnoreCase)) - { - absolutePath = absolutePath.Substring(pathBase.Length); - } - - var rawValue = absolutePath + var rawValue = new Uri($"{request.Scheme}://{request.Host}{rawUrl}").AbsolutePath // discard query string .Split('/', StringSplitOptions.RemoveEmptyEntries) .ElementAtOrDefault(index); -- cgit v1.2.3