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' });
}