I'm trying to pass a null argument to a web api controller but I'm getting "null" instead of null.
E.g : my route should be like this
[Route("api/student/GetStudent/{studentId}/{studentFname}/{studentLname}/")] public Student GetStudent(int studentId,string studentFname,string studentLname) { //Code } Note that at least user should insert first name or last name and isn't required to have both
In the above code , both firstname and lastname are required but I don't want this. So I change my code to be like this
[Route("api/student/GetStudent/{studentId}/{studentFname?}/{studentLname?}/")] public Student GetStudent(int studentId,string studentFname,string studentLname) { //Code }
As I said that when I call this method and pass a null argument for student firstname . I am getting "null" and when it pass to the database stored procedure it will pass as a value.
nullperhaps? In any case this code is wrong - GET is used to *request stuff, not send stuff to the server. Use POST instead."null"into the URL, then the model binder's response (giving you a"null"string) is the correct behavior.pass a null argument for student firstnameURLs are strings, they know nothing about nulls. If you call/api/student/GetStudent/1/null, you *have* specified a value forstudentFname` whose contents are"null". An optional parameter is one that doesn't have to be passed at all - useapi/student/GetStudent/1