I am using
- .NET Framework 4.6.1
- Microsoft.AspNet.WebApi 5.2.4
- ASP.NET uses Newtonsoft.Json 11.0.2
In Global.asax
I specify that I want to use the SnakeCaseNamingStrategy for my JSON serialization:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
...
var formatters = GlobalConfiguration.Configuration.Formatters;
// Remove the xmlformatter
formatters.Remove(formatters.XmlFormatter);
// Ignore reference loops
formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
// Use snake_case
formatters.JsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new SnakeCaseNamingStrategy()
};
}
}
This works great when returning OK
and most other success indicating status codes (200-300):
[HttpGet, Route("api/Test")]
public IHttpActionResult Test()
{
return Ok(new { Message = "Hello World!" });
}
Returns:
{
"message": "Hello World!"
}
But, when returning any error code or exception, ASP.NET seems to ignore any formatter settings:
[HttpGet, Route("api/Test")]
public IHttpActionResult Test()
{
return BadRequest("Hello World!");
}
Returns:
{
"Message": "Hello World!" // 'Message' is not snake_case
}
And
[HttpGet, Route("api/Test")]
public IHttpActionResult Test()
{
var runtimeErroredArray = new int [2];
runtimeErroredArray[2] = 5; // runtime error
return Ok();
}
Returns:
{
"Message": "An error has occurred.",
"ExceptionMessage": "Index was outside the bounds of the array.", // Should be 'exception_message'
"ExceptionType": "System.IndexOutOfRangeException", // "System.IndexOutOfRangeException" can stay the same obviously, but 'ExceptionType' should be 'exception_type'
"StackTrace": "---"
}
I don't understand why this happens, but I would mainly like to know how to solve it.
The question:
Is there a way I can force exception messages and error code messages follow the GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver
I set in Global.asax
(using SnakeCaseNamingStrategy
)?
EDIT:
I have tried setting JsonConvert.DefaultSettings
in Global.asax
, but that seemed to be ignored, even when returning an Ok
response now.
New code in Global.asax
:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
...
// Overwrite the default settings
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
// Ignore reference loops
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
// Use snake_case
ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
}
}
return Ok(new { Message = "Hello World!" });
(like in the first example) in action Test
now returns:
{
"Message": "Hello World!"
}
JsonConvert.DefaultSettings
Seems to be ignored completely by ASP.NET.