0

when using postsharp code contracts I have set a GreaterThan attribute to 0. I have an action with an id decorated with the GreaterThan contract

   public virtual ActionResult Edit([GreaterThan(0)] int id) 

when I call the http://localhost/Items/Edit/-1, I get the following error

The parameter 'id' must be greater than 0. Parameter name: id

when I call the http://localhost/Items/Edit/0 , I do not get that error.

It seems to be that the paramter is GreaterThan or equal to. I could set it to .5, but then my error message looks weird because it would say The parameter 'id' must be greater than 0.5

eiu165
  • 6,101
  • 10
  • 41
  • 59

2 Answers2

1

If it's a bug AND your number is going to be zero, then you could try using:

"StrictlyPositive", whihch Requires a value strictly greater than 0.

This is not a good solution, I know, but for now.

Nachokhan
  • 81
  • 7
1

This is caused by a bit of a naming clumsiness.

There are two constraints: GreaterThan and StrictlyGreaterThan. I think that this information clarifies it :-) - you have to use StrictlyGreaterThan in your use case.

Yes, "greater than" is a strict inequality and "greater than or equal" is a non-strict one, so naming of GreaterThan is incorrect.

Well, we know about it, but it's not possible to change APIs after they go public. So this funny mind bending issue is actually a feature...

Daniel Balas
  • 1,805
  • 1
  • 15
  • 20