0

I have a C# backend HttpGet method which is accepting a dictionary like this as request parameters.

public async Task<IActionResult> Search([BindRequired, FromQuery] IDictionary<string, object> pairs)

My frontend is in Angular 8. What should I pass in the Angular service class get method when backend is accepting a dictionary like this? Is it a Query String or what?

currently i am passing a query string like this,

search(parameters: any): Observable<any> {

var queryString = Object.keys(parameters).map(key => key + '=' + parameters[key]).join('&');

return this.httpClient.get(environment.host + environment.search + queryString);

}

But it is not working. when i debug and see, the backend request parameter values are null even though i passed values from frontend.

Thank You!

1 Answers1

0

This actually works as expected. How do you expect your application to deserialize a string into a base class? What would you expect to happen?

The way the default model binding works is that it iterates the fields on the target class and tries finding appropriate fields from the serialized data.

object contains no fields, so deserializing into it makes no actual sense.

If you have some intricate logic in which you would expect unknown data to be deserialized into your classes, you can provide a custom model binder. An example using reflection is pretty nicely covered in this SO answer.

But usually the best idea is to simply expect a non-dynamic data types in your API - even if the type would be rather generic.

TotallyNewb
  • 3,884
  • 1
  • 11
  • 16