Is it possible to handle receiving data in camel casing using OData?

In the Register method of the WebApiConfig class I have set 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-default JavaScript) to TitleCase (default that I use in properties on the server) and vice versa. p>

Ex. of JSON that the API receives in a PATCH normal method (without using Delta<T> )

    id: 1,
    nome: "Joaquim",
    idade: 150

This is "translated" into

    Id: 1,
    Nome: "Joaquim",
    Idade: 150
So far so good, the problem is that I have a action PATCH on a ApiController that makes use of

I have read in some places that OData uses a serializer and a deserializer itself and this should be the problem.

Method Delta<T> of PATCH :

[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();


    return StatusCode(HttpStatusCode.NoContent);

Is there any way to treat the " casing " of data received in ApiController using OData?

Am I going to need to write a new serializer or is there something ready that can help me with this? If you need to write a serializer, can you help me with at least the basic implementation of it?

JSON example I get in method with actions

{ nome: "Teste método PATCH" }

If I send JSON, like the one below, it works fine.

{ Nome: "Teste método PATCH" }

Note: I'm using OWIN, but the application is being hosted on IIS (using the Delta<T> ) package.

asked by anonymous 25.05.2016 / 15:37

2 answers


I believe this will solve the problem:

Apply this [CamelCasingFilter] attribute to any action you want to camel casing.

public class CamelCasingFilterAttribute : ActionFilterAttribute
    private JsonMediaTypeFormatter _camelCasingFormatter = new JsonMediaTypeFormatter();

    public CamelCasingFilterAttribute()
        _camelCasingFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        ObjectContent content = actionExecutedContext.Response.Content as ObjectContent;
        if (content != null)
            if (content.Formatter is JsonMediaTypeFormatter)
                actionExecutedContext.Response.Content = new ObjectContent(content.ObjectType, content.Value, _camelCasingFormatter);

Source: link

02.01.2018 / 12:20

You can use the EnableLowerCamelCase extension method:

Using System.Web.OData.Builder;
Using System.Web.OData.Extensions;

Var builder = new ODataConventionModelBuilder ();
Builder.EnableLowerCamelCase ();

See also: link .

14.05.2017 / 01:22