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.

9
  • 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. Commented Oct 16, 2019 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. Commented Oct 16, 2019 at 10:21
  • This is the opposite problem. 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. Commented Oct 16, 2019 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 Commented Oct 16, 2019 at 10:22
  • By the way, if you always expect id why do you ask for first and last name? Commented Oct 16, 2019 at 10:28

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

0

I suppose you should call your API with below format:

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

1 Comment

I know this behavior but I want to pass them like this /something/something

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.