0

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.

  • Because having your url constructed as "/api/student/getStudent/123/null/null" will still evaluate to string values. How would it know that my surname is in fact not written as "Null"? Pass a model in, rather than values in segments and problem will be solved. – Adrian Oct 16 '19 at 10:20
  • What does the URL look like? Did you *actually pass the string `null` perhaps? In any case this code is wrong - GET is used to *request* stuff, not send stuff to the server. Use POST instead. – Panagiotis Kanavos Oct 16 '19 at 10:21
  • [This is the opposite problem](https://stackoverflow.com/questions/4456438/how-to-pass-null-a-real-surname-to-a-soap-web-service-in-actionscript-3). I think the issue in this case isn't that the behavior is wrong, but that your expectation is wrong. If you literally put a `"null"` into the URL, then the model binder's response (giving you a `"null"` string) is the **correct** behavior. – Flater Oct 16 '19 at 10:22
  • `pass a null argument for student firstname` URLs are strings, they know nothing about nulls. If you call `/api/student/GetStudent/1/null, you *have* specified a value for `studentFname` whose contents *are* `"null"`. An optional parameter is one that *doesn't* have to be passed at all - use `api/student/GetStudent/1` – Panagiotis Kanavos Oct 16 '19 at 10:22
  • By the way, if you always expect id why do you ask for first and last name? – tymtam Oct 16 '19 at 10:28
  • @tymtam I'm searching for student by his firstname or lastname , I am using Id for something else – John Not Travolta Oct 16 '19 at 11:28
  • @JohnNotTravolta - I think it's been directly or indirectly said a few times above, but I think you should first invest a considerable amount of time researching what makes good REST API design. A couple things that I notice immediately: 1) A resource based API would just say "api/student/{studentId}...". The "getStudent" section is reduntant - this is the role of HTTP Verbs. 2) you're resource identifier for student seems to be "studentId". That should be enough information to get the student's name. (1/2) – chill94 Oct 16 '19 at 12:02
  • Rather, if you want to get a student using *only* the name, maybe you can have an endpoint, "api/students" that returns all students, but which also accepts some filter url parameters. [Here's](https://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/) the first link that google shows for REST API design principle, which supports both of these notes... but, don't take my word for it! Look around, research different API designs, and choose whichever works best for you! (2/2) – chill94 Oct 16 '19 at 12:02
  • The problem having studentId and studentName in the same URL request is now you have to handle what to do when the name sent to the server doesn't match the name which corresponds to that studentId stored in the database. And since I don't even know what the expected behavior should be, it's natural that your API clients wouldn't know either. Definitely not an example of creating a [pit of success](https://medium.com/@ricomariani/the-pit-of-success-cfefc6cb64c8) – chill94 Oct 16 '19 at 12:05

2 Answers2

1

This is most likely because the method is called as

api/student/GetStudent/xxx/null/something

null in this case is provided and is in fact "null".

You may need to expose

  • api/student/GetStudentByLastName/{lname}
  • api/student/GetStudentByFirstName/{fname}
  • api/student/GetStudentById/{id}

Depending on your setup you may be able to do

  • api/student/GetStudent/IdHere?fname=xxx (lname will be null)
  • api/student/GetStudent/IdHere?lname=xxx (fname will be null)

(btw. I'm not sure why you pass the name parts, if id is required)

tymtam
  • 20,472
  • 3
  • 58
  • 92
0

I suppose you should call your API with below format:

api/student/GetStudent/studentId=&studentFname=&studentLname=

dilipkumar1007
  • 143
  • 3
  • 20