[HttpPost, Route("foo")]
public void DoStuff(FooModel args)
{
if(args == null) args = new FooModel();
// ...
}
public class FooModel
{
public string Foo { get; set; }
public string Bar { get; set; }
}
When a POST request is sent to a Web API method and the HTTP request does not include any of the arguments that would populate the model, instead of passing an empty model to the action, Web API instead just passes null to the action, necessitating a null check against the model argument at the start of every action.
I understand that the framework is probably trying to avoid an unnecessary object construction, but these are always lightweight classes with no specific functionality. It would save time and be more consistent if the model was constructed every time rather than trying to save some negligible fraction of a millisecond that would be the cost of just constructing the model object anyway and passing it to the action.
How can I get the framework to always pass a constructed (non-null) model object to my actions, even if none of the associated properties were able to be populated?
I'm using ASP.Net Web API 2.2.