OData JSON camelCase
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OData JSON camelCase相关的知识,希望对你有一定的参考价值。
我想将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);
}
}
答案
您可以使用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。
另一答案
此示例可能符合您的要求,只需从此文件开始:
这些样本后来移动了here。
以上是关于OData JSON camelCase的主要内容,如果未能解决你的问题,请参考以下文章
JSON 命名约定(snake_case、camelCase 或 PascalCase)[关闭]
SignalR .NET Core camelCase JSON 合同解析器
如何在 JSON 响应 ASP.NET Core 中关闭或处理 camelCasing?