51
In the method Register
class WebApiConfig
i have configured a CamelCasePropertyNamesContractResolver
public static void Register(HttpConfiguration config)
{
//Resto do código removido para brevidade
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
This causes the data received (or sent) by JSON to be converted from camelCase
(client standard - Javascript) for TitleCase
(default that I use in the server properties) and vice versa.
Ex. of JSON that the API receives in a method PATCH
normal (without using the Delta<T>
)
{
id: 1,
nome: "Joaquim",
idade: 150
}
This is "translated" to
{
Id: 1,
Nome: "Joaquim",
Idade: 150
}
So far so good, the problem is that I have a action
PATCH
in a ApiController
who makes use of the Delta<T>
and the requests that are sent to this method end up not going through this "translation".
I read in some places that Odata uses a serializer and an deserializer of his own and this must be the problem.
Method PATCH
of ApiController
:
[HttpPatch, ResponseType(typeof(void))]
public async Task<IHttpActionResult> Patch(int id, [FromBody] Delta<Entity> changes)
{
var entity = await _db.Entities.FindAsync(id);
if (entity == null)
return NotFound();
changes.Patch(entity);
_db.SaveChanges();
return StatusCode(HttpStatusCode.NoContent);
}
Is there any way to treat the "Casing" of the data received in actions
who use Odata?
Do I need to write a new serializer or do you already have something ready that can help me with that? If you need to write a serializer, you can help me with at least the basic implementation of it?
Example of JSON I get in the com method Delta<T>
{ nome: "Teste método PATCH" }
If I send the JSON, as the below, works normal.
{ Nome: "Teste método PATCH" }
Note: I am using OWIN, but the application is being hosted on IIS (using the package (Microsoft.Owin.Host.SystemWeb
).
There’s an ASP.NET sampling site that has a project to address the serialization of
Delta<T>
. Maybe it’ll help you.– Pagotti