How do I handle null values during model binding

227 Views Asked by At

I have the folowing URL:

http://localhost:7975/test?parameter01=X

In my model, parameter01 is a List<int?>. If a non-integer value (e.g., a string) is passed to this parameter, the model binding process sets this value to null.

How do I intercept this as early as possible in the pipeline so I can return a HTTP status and description without handling this condition in the controller action?

1

There are 1 best solutions below

0
On

Since you have tagged with asp.net-web-api2 I would recommend using Attribute Routing which enables you to constrain the Parameter Type. With this you'd able to switch handling according to the validity of your input. You can read up on this here

A second possibility would be to write a HTTPHandler which tests for valid input information. This one might be a bit trickier.