1
votes

I have an issue where in a class using automatic attributes Json.Net adds k__BackingField to attribute names.

I looked at various recommendation (ie adding [JsonObject]) and none of them works for me.

I found one recommended solution is to do:

((Newtonsoft.Json.Serialization.DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;

(https://stackoverflow.com/a/36910449/460084)

However I don't know where or how to do this in .NET Core 2.1 ? does this goes in Startup.cs ? how ?

Also I am not even sure that will help, as my class does not have [Serializable] to begin with.

Any help ? Is there really no simple way to change the default for Json.Net to use attribute names as is without the k__BackingField in .NET Core 2.1

1

1 Answers

1
votes

Add it to the Startup.cs in ConfigureServices method:

// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc()
        .AddJsonOptions(options => (options.SerializerSettings.ContractResolver as DefaultContractResolver).IgnoreSerializableAttribute = true);

    // ...
}