Custom Model Binder Provider always null .net core

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Custom Model Binder Provider always null .net core



I'm having a problem trying to get custom model binders to work as a query parameter like I have gotten to work previously in .net framework 4.7.



To ensure this wasn't a scenario where my object was too complex, I reduced the model to a simple string but even then I cannot get this to work.



I have a simple model I would like to be binded from query parameters.


public class SearchModel {
public string SearchTerms { get; set; }
}



And I have configured the ModelBinder and ModelBinderProvider as shown here like so.


public class TestModelBinder : IModelBinder {
public Task BindModelAsync(ModelBindingContext bindingContext) {
if (bindingContext.ModelType != typeof(SearchModel)) {
throw new ArgumentException($"Invalid binding context supplied {bindingContext.ModelType}");
}

var model = (SearchModel)bindingContext.Model ?? new SearchModel();

var properties = model.GetType().GetProperties();
foreach(var p in properties) {
var value = this.GetValue(bindingContext, p.Name);
p.SetValue(model, Convert.ChangeType(value, p.PropertyType), null);
}

return Task.CompletedTask;
}

protected string GetValue(ModelBindingContext context, string key) {
var result = context.ValueProvider.GetValue(key);

return result.FirstValue;
}
}

public class TestModelBinderProvider : IModelBinderProvider {
public IModelBinder GetBinder(ModelBinderProviderContext context) {
if (context == null) {
throw new ArgumentNullException(nameof(context));
}

if (context.Metadata.ModelType == typeof(SearchModel)) {
var returnType = new BinderTypeModelBinder(typeof(TestModelBinder));
return returnType;
}

return null;
}
}



As stated in the last step in Microsoft documentation I updated my ConfigureServices method in Startup.cs to include the BinderProvider.


services.AddMvc(options => {
options.ModelBinderProviders.Insert(0, new TestModelBinderProvider());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);



But when I call my Search endpoint with a url such as "https://localhost:44387/api/testbinding?searchTerms=newSearch" I am always seeing a return of "request == null True" even though I see it properly hit the custom binding and bind correctly if I step through debugging, can anyone please point me in the right direction as to what I am doing wrong?


[Route("api/[controller]")]
[ApiController]
public class TestBindingController : ControllerBase {

[HttpGet()]
public IActionResult GetResult([FromQuery] SearchModel request) {
return Ok($"request == null {request == null}");
}

}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Will Oldham

Makefile test if variable is not empty