Skip to content

Commit

Permalink
Cherry pick branch 'genexuslabs:fix/empty-string-rest-query-parm' int…
Browse files Browse the repository at this point in the history
…o beta
  • Loading branch information
claudiamurialdo authored and Beta Bot committed Jan 20, 2025
1 parent a6320d7 commit bb8a4b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
33 changes: 33 additions & 0 deletions dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRestServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Threading.Tasks;


namespace GeneXus.Utils
Expand All @@ -29,6 +32,36 @@ public void OnActionExecuting(ActionExecutingContext context)
(context.Controller as GxRestService).Initialize();
}
}

public class QueryStringModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context.BindingInfo.BindingSource == BindingSource.Query &&
context.Metadata.ModelType == typeof(string))
{
return new BinderTypeModelBinder(typeof(CustomQueryStringBinder));
}
return null;
}
}
public class CustomQueryStringBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (!bindingContext.BindingSource.CanAcceptDataFrom(BindingSource.Query))
{
return Task.CompletedTask;
}
if (bindingContext.ModelType != typeof(string))
{
return Task.CompletedTask;
}
string value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).FirstValue;
bindingContext.Result = ModelBindingResult.Success(value ?? string.Empty);
return Task.CompletedTask;
}
}
[TypeFilter(typeof(CustomActionFilter))]
public class GxRestService : ControllerBase
{
Expand Down
8 changes: 6 additions & 2 deletions dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,13 @@ public void ConfigureServices(IServiceCollection services)
private void RegisterControllerAssemblies(IMvcBuilder mvcBuilder)
{

if (RestAPIHelpers.ServiceAsController() && !string.IsNullOrEmpty(VirtualPath))
if (RestAPIHelpers.ServiceAsController())
{
mvcBuilder.AddMvcOptions(options => options.Conventions.Add(new SetRoutePrefix(new RouteAttribute(VirtualPath))));
mvcBuilder.AddMvcOptions(options => options.ModelBinderProviders.Insert(0, new QueryStringModelBinderProvider()));
if (!string.IsNullOrEmpty(VirtualPath))
{
mvcBuilder.AddMvcOptions(options => options.Conventions.Add(new SetRoutePrefix(new RouteAttribute(VirtualPath))));
}
}

if (RestAPIHelpers.JsonSerializerCaseSensitive())
Expand Down

0 comments on commit bb8a4b8

Please sign in to comment.