John Not Travolta
John Not Travolta

Reputation: 1

Receiving "null" instead of null

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

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.

Upvotes: 0

Views: 230

Answers (2)

tmaj
tmaj

Reputation: 34947

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)

Upvotes: 1

dilipkumar1007
dilipkumar1007

Reputation: 363

I suppose you should call your API with below format:

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

Upvotes: 0

Related Questions