I have a document which has a field:
{
FOB: "..."
}
However when I return it:
[HttpGet("{documentRef}")]
public ActionResult<Ten> Get(string documentRef)
{
var data = _service.FindOneTen(documentRef);
if (data != null) return data;
Response.StatusCode = 400;
return NotFound();
}
It is converted to this:
{
fob: "..."
}
How can I stop this happening / Why is this happening?
I've seen examples of people using
PropertyNamingPolicy = null;
To disable it for MVC / the normal JSON which I tried and didn't work.
Startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson();
}
Edit
While Objects exposed on JSON web-api - how to stop the property names changing case? is solving the problem in one way, I would rather not need to specify for every field in the model.
CodePudding user response:
use this
services.AddControllers().AddNewtonsoftJson(options=>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver() { };
});
CodePudding user response:
If you're using .net core. You can try use default .net core serializer, it should preserve the original casing.
System.Text.Json.JsonSerializer.Serialize()
And also, you need to config in Startup.ConfigureServices()
services.AddMvc().AddJsonOptions(o => o.JsonSerializerOptions.PropertyNamingPolicy = null);
If you need to use Json.net then use this
services.AddMvc().AddNewtonsoftJson(o =>
{
o.SerializerSettings.ContractResolver = new DefaultContractResolver();
});