4

I am using [DataMember(Name="property_name")] attributes for properties on my models which are decorated with [DataContract]. In addition to that I use the [Required] attribute on each required property for validation. I have a registered an ActionFilterAttribute which checks if the ModelState.IsValid and creates an error response:

public override void OnActionExecuting(HttpActionContext actionContext)
{            
    if (!actionContext.ModelState.IsValid)
    {
        actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
    }
}

This is all working well, however the DataMember Name is not being returned in the response object:

{
  "Message": "The request is invalid.",
  "ModelState": {
    "f.Message": [
      "The Message field is required."
    ],
    "f.AppVersion": [
      "The AppVersion field is required."
    ]
  }
}

In the above example you can see that the DataMember Name is being ignored. What I am looking for is a response more of the likes of:

{
  "Message": "The request is invalid.",
  "ModelState": {
    "f.message": [
      "The message field is required."
    ],
    "f.app_version": [
      "The app_version field is required."
    ]
  }
}

i.e. with the property names matching my DataMember Name rather than the actual class declaration.

Any guidance would be appreciated

Many thanks

Lukas
  • 694
  • 1
  • 9
  • 24

0 Answers0