Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData JSON camelCase

I want to use the CamelCasePropertyNamesContractResolver for the OData JSON response. How can I achieve this. With the follwoing Code Fragment only the webapi calls are considered.

regards

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // New code:
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Configuration>("Configurations");
        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: "odata",
            model: builder.GetEdmModel());

        config.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        config.Formatters.Remove(config.Formatters.XmlFormatter);
    }
}

Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();


        var config = GlobalConfiguration.Configuration;
        var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
        config.Formatters.JsonFormatter.SerializerSettings = settings;

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}
like image 670
Marko Avatar asked Jul 23 '14 11:07

Marko


Video Answer


1 Answers

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: http://msdn.microsoft.com/en-us/library/dn746676%28v=vs.118%29.aspx.

like image 103
Torben Koch Pløen Avatar answered Sep 24 '22 18:09

Torben Koch Pløen