OData JSON camelCase

I want to use CamelCasePropertyNamesContractResolver to respond with an OData JSON. How can i achieve this. With the following code snippet, only webapi calls are considered.

considers

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); } } 
+7
json odata asp.net-mvc asp.net-web-api2
source share
2 answers

You can use the EnableLowerCamelCase extension EnableLowerCamelCase :

 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 .

+8
source share

This example may suit your requirements, just start with this file:

Since then, samples have been moved here .

0
source share

All Articles