asp.net web api - Web api validation on nullable parameter fails -


i trying validate simple method , getting value 'null' not valid nullable`1 error.

    [validatemodel]     public ienumerable<somedata> get(bool? showextra = null)     {         return this.mockdatamanager.showdata(showextra);     } 

validatemodel property is:

 public override void onactionexecuting(httpactioncontext actioncontext)     {         if (actioncontext != null && actioncontext.modelstate.isvalid == false)         {             actioncontext.response = actioncontext.request.createerrorresponse(                 httpstatuscode.badrequest, actioncontext.modelstate);         }     } 

now, if call method /true , /false works. works if call method / if call /null validation fails , error message value 'null' not valid nullable`1 appears. how resolve this?

calling / way go. calling /null not want do.

here's happening behind scenes:

/false ok, let's see if can convert bool. yes, can.  /true ok, let's see if can convert bool. yes, can.  / ok, let's see if can convert bool. no, can't, use default value specified in method arguments.  /null ok, let's see if can convert bool. no, can't, throw exception. 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -