Reputation: 1
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.
Upvotes: 0
Views: 230
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
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
Reputation: 363
I suppose you should call your API with below format:
api/student/GetStudent/studentId=&studentFname=&studentLname=
Upvotes: 0