1

I'm using WebApi model validation to validate my request to only accept integer in the body by doing this:

[Range(0,10)]
public int AwesomeLevel { get; set; }

But if i send in a string value, the validation does not fail and the AwesomeLevel is set to 0.

AwesomeLevel="NotValid"

How can I make sure my validation will fail when a non integer value, in this case a string, is sent for this property?

1 Answer 1

3

Try changing your model to be

[Required, Range(0, 10)]
public int? AwesomeLevel { get; set; }

This should force the client to set a valid value for the property. See here for more details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.