How to change/customize parameter name on web api 2 error reponse

Topics: ASP.NET Web API
Jan 29, 2014 at 8:06 PM
I posted on stackoverflow at : http://stackoverflow.com/questions/21441320/how-to-change-customize-parameter-name-on-web-api-2-error-reponse

and then realized that maybe this should be a better place.

Having this action inside a WebApiController :
public IHttpActionResult Post(PostBindingModel binding) {
    if (!ModelState.IsValid)
        return BadRequest(ModelState);
// ....
}
An example error is :
{
  "message": "The request is invalid.",
  "modelState": {
    "binding.Items[0].ProductName": [
      "The ProductName field is required."
    ]
  }
}
as you can see, the binding property is shown on the error. Is there anyway to change this as the binding is not show or at least change it's name to something else ? because from the client perspective, binding gives him no clue, there's is not such thing for the client on their context, so using binding here just make some noise.

I can see why it's there, I'm asking if it's possible to have it removed. Also, if I change my mind later, and modify the binding to be called request or model, or bindingmodel, I will break any client parsing the errors using the 'binding' part, making me to stick to a strict naming with no value for the customer and I think it should't be in the response in first place.

Hope it's clear enough.

just for the sake of clarity why binding does not exist from the client perspective, here's a sample request :

{
  "items": [
    {
      "quantity": 3,
    },
    {
      "productName": "sample string 1",
      "quantity": 3,
    }
}
p.d.: also modelstate makes noise... would be great if one can set as something like 'errors'.

a desired customized error response would be something like :
{
  "message": "The request is invalid.",
  "errors": {
    "Items[0].ProductName": [
      "The ProductName field is required."
    ]
  }
}
If this cannot be done right now, I would like it to propose as a feature for webapi 2.x