Web api validation on nullable parameter fails

后端 未结 1 1987
轮回少年
轮回少年 2021-01-26 16:37

I am trying to validate very simple method and I am getting The value \'null\' is not valid for Nullable`1 error.

    [ValidateModel]
    public IEnumerable

        
1条回答
  •  天涯浪人
    2021-01-26 16:51

    Calling it with / is the way to go. Calling it for /null is not what you want to do.

    Here's what is happening behind the scenes:

    /false
    OK, let's see if I can convert that into a bool. Yes, I can.
    
    /true
    OK, let's see if I can convert that into a bool. Yes, I can.
    
    /
    OK, let's see if I can convert that into a bool. No, I can't, so use the default
    value specified in the method arguments.
    
    /null
    OK, let's see if I can convert that into a bool. No, I can't, so throw an
    exception.
    

    0 讨论(0)
提交回复
热议问题