我想将CamelCasePropertyNamesContractResolver用于OData JSON响应。我怎样才能做到这一点。使用以下代码片段仅考虑webapi调用。
问候
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);
}
}
答案 0 :(得分:13)
您可以使用EnableLowerCamelCase
扩展名方法:
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
var builder = new ODataConventionModelBuilder();
builder.EnableLowerCamelCase();
另请参阅:http://msdn.microsoft.com/en-us/library/dn746676%28v=vs.118%29.aspx。
答案 1 :(得分:0)
此示例可能符合您的要求,只需从此文件开始:
此后样本已移动here。