I have a project on Asp.net Web Api 2. I faced kind of interesting behavior of .net framework during testing one of the endpoint.
this is the simplified version of endpoint:
[Route("api/v1/bookings/documenttype/{clientId}/{companyCode}/{year}")]
[HttpGet]
[ResponseType(typeof(IEnumerable<UserVoucherType>))]
public IHttpActionResult GetUserDocumentTypes(int clientId, string companyCode,
int year, [FromUri] int? month = null)
{
//Do Some work
return Ok();
}
the problem here when I send a non integer value as value of 'month' parameter instead of getting bad request or something like this I get null during run time and this cause problem. For example one of the consumer of endpoint were sending '[1,2,3]' and getting all data for whole year instead of getting an error message.
For example for this request I am getting below values that I pointed on ss:
http://localhost:64652/api/v1/bookings/documenttype/1/0001/2019?month=[1,2,3]
Now my question how can I return a bad request for a case like this.
The month parameter is optional and I can't understand on endpoint did consumer send this parameter null on purpose or they send a not valid value for parameter.
Is there a setting for this ? I was thinking to write a custom paramater binder but isn't this over engineering ? I think the behavior of framework should be default returning bad request or am I missing something?