简体   繁体   English

如何在使用 MinimalApi 时配置 json Name Case 策略

[英]How to configure json Name Case policy while using MinimalApi

I'm trying to get result from my minimal API who configured in endpoints of my MVC web application my Get action configured like this:我正在尝试从我的最小 API 获取结果,这些 API 在我的 MVC Web 应用程序的端点中配置,我的 Get 操作配置如下:

            endpoints.MapGet(
                "HO-CFDZU4/api/Currency/Get", 
                [PermissionAuthorize(PermissionName.ReadCurrencyDictionary)] 
                async ([FromServicesAttribute] CurrencyService curency) =>
            {
                var result = await DataSourceLoader.LoadAsync(curency.Get(), new DataSourceLoadOptions());

                return Results.Ok(result);
            });

As result i get response with object where property names changed to lowercase, and its not suit for me.结果,我得到了属性名称更改为小写的对象的响应,它不适合我。 I want to get exactly same name in same case like i return form action.我想在相同的情况下获得完全相同的名称,就像我返回表单操作一样。

To get similar effect in MVC i used this code:为了在 MVC 中获得类似的效果,我使用了这段代码:

            services
            .AddMvc()
            .AddFluentValidation(x => x.RegisterValidatorsFromAssembly(AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName.Contains("ApplicationCore")).Single()))
            .AddMvcLocalization()
            .AddMvcOptions(options =>{})
            .AddRazorRuntimeCompilation()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
                options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
            });

Which setup property naming policy for Json while using action in controllers, and i dont know how to setup same policy for minimalApi.在控制器中使用操作时为 Json 设置属性命名策略,我不知道如何为 minimalApi 设置相同的策略。

What Ive tried is to set [JsonPropertyName(name)] And it working good but we have lot of classes and i looking for more global solution.我试过的是设置[JsonPropertyName(name)]并且效果很好,但我们有很多类,我正在寻找更全局的解决方案。

I also tried configure JsonOptions globally like this:我还尝试像这样全局配置 JsonOptions:

        services.Configure<JsonOptions>(options =>
        {
            options.JsonSerializerOptions.PropertyNamingPolicy = null;
            options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
        });

But it do nothing但它什么都不做

Use JsonOptions from Microsoft.AspNetCore.Http.Json namespace ( docs ):使用Microsoft.AspNetCore.Http.Json命名空间( 文档)中的JsonOptions

builder.Services.Configure<JsonOptions>(options =>
{
    options.SerializerOptions.PropertyNamingPolicy = null;
    options.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 .NET 6.0 中使用 MinimalApi 配置 NewtonsoftJson - How to configure NewtonsoftJson with MinimalApi in .NET 6.0 如何使用c ++配置Windows组策略? - How to configure Windows Group Policy using c++? 当我使用区分大小写的语言时,如何解决COM中不区分大小写的冲突名称 - How to solve case insensitive conflict name in COM while I using a case sensitive language 如何使用 c# 配置密码策略(组策略) - How can I configure Password Policy(Group Policy) using c# 在 ASP.NET Core 3 中是否有内置的使用 snake case 作为 JSON 命名策略的方法? - Is there a built in way of using snake case as the naming policy for JSON in ASP.NET Core 3? 如何在使用Docker时为NuGet配置代理? - How to configure proxy for NuGet while using Docker? 如何使用 json 配置 Serilog 日志记录到 Cloudwatch - How to Configure Serilog Logging to Cloudwatch using json 使用 Poly 重试策略时如何处理 100 秒超时 - How to deal with 100 seconds timeouts while using Poly retry policy 如何将AutoMapper配置为区分大小写? - How to configure AutoMapper to be case sensitive? 如何配置WebJob ServiceBusTrigger重试策略 - How to configure WebJob ServiceBusTrigger retry policy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM