【问题标题】:OData defaults to pascal case when using $select or $filter使用 $select 或 $filter 时 OData 默认为帕斯卡大小写
【发布时间】:2021-01-03 17:48:21
【问题描述】:

在我的 .net core 3.1 web api 项目中,我使用了Microsoft.AspnetCore.OdataMicrosoft.AspnetCore.Mvc.NewtonsoftJson。我的创业公司是这样的:

    services.AddOData();
            services.AddControllers(options =>
            {
                IEnumerable<ODataOutputFormatter> outputFormatters =
                    options.OutputFormatters.OfType<ODataOutputFormatter>()
                        .Where(foramtter => foramtter.SupportedMediaTypes.Count == 0);

                foreach (var outputFormatter in outputFormatters)
                {
                    outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/odata"));
                }

            }).AddNewtonsoftJson(options =>
            {
                options.UseCamelCasing(false);
                options.SerializerSettings.ContractResolver = new DefaultContractResolver() { NamingStrategy = new CamelCaseNamingStrategy() };
            });
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
app.UseEndpoints(endpoints =>
            {
                endpoints.MapHealthChecks("/health");
                endpoints.MapControllers().RequireAuthorization();
                endpoints.EnableDependencyInjection();
                endpoints.Select().OrderBy().Filter();
                endpoints.MapODataRoute("odata", "odata", GetEdmModel());
            });
}

IEdmModel GetEdmModel()
        {
            var builder = new ODataConventionModelBuilder();
            builder.EnableLowerCamelCase();
            builder.EntitySet<Ebp>("Ebp");
            return builder.GetEdmModel();
        }

当我使用{{baseUrl}}/odata/coaching/ebp/all 调用GET 端点时,结果在camelCase 中,正如我所料。

但是,当我引入任何$select$filter 选项进行查询时,我的回复将更改为PascalCase

为什么会这样?

【问题讨论】:

    标签: asp.net-core json.net odata asp.net-core-webapi


    【解决方案1】:

    答案在this post。在附加到服务集合的AddNewtonSoftJson 方法中,我应该将SerializerSettings.ContractResolver 设置为CamelCasePropertyNamesContractResolver

    services.AddOData();
            services.AddControllers(options =>
            {
                IEnumerable<ODataOutputFormatter> outputFormatters =
                    options.OutputFormatters.OfType<ODataOutputFormatter>()
                        .Where(foramtter => foramtter.SupportedMediaTypes.Count == 0);
    
                foreach (var outputFormatter in outputFormatters)
                {
                    outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/odata"));
                }
    
            }).AddNewtonsoftJson(options =>
            {
                options.UseCamelCasing(false);
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            });
    

    【讨论】:

    • 我在为我们的问题苦苦挣扎,当我使用“$expand”查询选项时,这解决了我的问题
    猜你喜欢
    • 2015-10-10
    • 2020-12-22
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    相关资源
    最近更新 更多