1

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 :

            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 :

            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.

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.

I also tried configure JsonOptions globally like this:

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

But it do nothing

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Igor Markiv
  • 147
  • 12
  • You return Results.Ok(), if you want formatted json to return use Results.Json() which has optional JsonSerializerOption parameter – Ramin Quliyev Dec 23 '22 at 07:48

1 Answers1

2

Use JsonOptions from Microsoft.AspNetCore.Http.Json namespace (docs):

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

UPD

If your application uses both Minimal APIs endpoints and MVC ones, then you try to configure options from both namespaces:

services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(options =>
{
    options.SerializerOptions.PropertyNamingPolicy = null;
    options.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
services.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = null;
    options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • As I said I already tried similar solution and its do not working for me. And your code snipped do not suit me because I use MVC Core Web application not MinimalApi solution, and in my case services configured just like I pasted in original post. – Igor Markiv Dec 23 '22 at 07:33
  • @игорьмаркив _"I use MVC Core Web application not MinimalApi solution"_ - your question explicitly says _"while using MinimalApi"_. If you need to use both Minimal APIs and MVC endpoints then setup `JsonOptions` from both namespaces using fully qualified names. – Guru Stron Dec 23 '22 at 07:38
  • I mean MinimalApi declaration in MVC WEB Route Endpoints that clearly seen from Code snipped what i provider in original post, but i don't use Separate MInimalApi Solution, and as i says i know how to set JsonOptions in services using IMVCbuilder but i dont know how to configure same thing using IEndpointRouteBuilder – Igor Markiv Dec 23 '22 at 07:48
  • Your Answer Working after small changes : builder.Services should be changed to services.Configure – Igor Markiv Dec 23 '22 at 07:57