I'm using ASP.NET's ApiController
class to create a Web API. I find that if I pass invalid JSON, instead of the caller getting a 500, the input parameter is null. As in, if I pass
{ "InputProperty:" "Some Value" }
which is clearly not valid, to this method:
[HttpPost]
public Dto.OperationOutput Operation(Dto.OperationInput p_input)
{
return this.BusinessLogic.Operation(p_input);
}
I get that p_input
is null
. I'd rather send something back telling the user they didn't POST valid JSON.
In my WebApiConfig.cs
, I have:
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
config.Formatters.XmlFormatter.UseXmlSerializer = true;
Any ideas? I did see this example, but I believe that's ASP.NET MVC, not ApiController.
edit: I've made the output from the class a little more specific and changed the status code. I'd started making these changes and later saw @CodeCaster's second comment.
original answer: Thanks to a pointer from @CodeCaster, I am using the following, and it seems to work: