We are migrating a .NET 2.2 app to .NET 6, and we are facing a problem where some third-party apps that make requests to our system without specifying the
Accept HTTP header were getting back JSON data by default in .NET 2.2, but now they are getting XML when nothing is specified.
If I run the same request, but specify
application/json, I get back JSON.
Our app returns 99% of the results in JSON, but has a specific SOAP endpoint that returns XML (it communicates with another SOAP web service and returns SOAP on this single endpoint).
The config is:
services.AddSoapCore(); services .AddMvc() .AddXmlSerializerFormatters() .AddNewtonsoftJson(options => options.SerializerSettings.Converters.Add(new StringEnumConverter()));
If I remove
AddXmlSerializerFormatters(), the endpoint returns JSON correctly but then the SOAP endpoint stops working.
I am aware I can use the
[Produces] attribute, but ideally we would keep the same default behavior instead of having to manually go through all endpoints.
Is there a way to configure the app to return JSON by default when nothing is specified, without removing XML support?
Leave an answer