gpt4 book ai didi

asp.net-mvc - OData JSON 驼峰命名法

转载 作者:行者123 更新时间:2023-12-02 02:32:00 25 4
gpt4 key购买 nike

我想使用 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 .

关于asp.net-mvc - OData JSON 驼峰命名法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24909532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com