0

This seems like a duplicate, but I've tried every permutation of the other answers I can come up with, and WebAPI always fails to bind a parameter of type Dictionary<string, string>. The parameter values is never populated. It usually is instantiated as an empty dictionary, though some variation I tried (lost track which) it was just null. Have tried various data shapes (see javascript snip), and both GET and POST verbs, changing the parameter attribute between [FromUri] and [FromBody] as appropriate.

Controller Method

[Route("api/reports/generate/{reportid}")]
[HttpPost]
public object GenerateReport(Guid reportid, [FromBody] Dictionary<string, string> values) {
  // logic snipped...
}

Angular Service (though I have tried the same requests from Postman)

runReport(reportid: string, values: any) {
    const keys = Object.keys(values);

    // shape: { "values": { "someKey": "some string value", "anotherKey": "value" } }
    const params: any = { values: values}; 

    // shape: {"values": [{key: "someKey", value: "some string value"}, ...]}
    // params.values = keys.map(k => ({ key: k, value: values[k] }));

    // shape: { "values[0].Key": "someKey", "values[0].Value": "some string value", ...}
    // keys.forEach((k, i) => {
    //   params[`values[${i}].Key`] = k;
    //   params[`values[${i}].Value`] = values[k];
    // });

    // try as get request (with parameter as [FromUri])
    // return this._http.get(`/api/reports/generate/${reportid}`, {
    //   params, responseType: 'blob'
    // });

    // try as POST request (with parameter as [FromBody])
    return this._http.post(`/api/reports/generate/${reportid}`, params, { responseType: 'blob' });
  }
BLSully
  • 5,929
  • 1
  • 27
  • 43
  • have you tried stringy on the paramts when sending? – Nkosi Jan 03 '18 at 20:41
  • Try to find solution [here](http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx) or [here](https://stackoverflow.com/questions/1077481/how-do-i-pass-a-dictionary-as-a-parameter-to-an-actionresult-method-from-jquery) – OlegI Jan 03 '18 at 20:42
  • I used a view model with one of the properties being a Dictionary, and that works just fine, so I know the Web API knows how to deserialize it, it just doesn't seem to want to do it as a standalone parameter type. – BLSully Jan 03 '18 at 22:24

1 Answers1

0

Quite late, but try this -

[Route("api/reports/generate/{reportid}")]
[HttpPost]
public object GenerateReport(Guid reportid) {
  var values = this.Url.Request.GetQueryNameValuePairs(); //This would return the values in Key-Value dictionary format.
}
Raj
  • 127
  • 2
  • 9