简体   繁体   English

接收“null”而不是 null

[英]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. I'm trying to pass a null argument to a web api controller but I'm getting "null" instead of null.

Eg: 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.正如我所说,当我调用此方法并为学生名字传递 null 参数时。 I am getting "null" and when it pass to the database stored procedure it will pass as a value.我得到“null”,当它传递给数据库存储过程时,它将作为一个值传递。

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". null 在这种情况下是提供的,实际上是“null”。

You may need to expose你可能需要暴露

  • api/student/GetStudentByLastName/{lname} api/学生/GetStudentByLastName/{lname}
  • api/student/GetStudentByFirstName/{fname} api/student/GetStudentByFirstName/{fname}
  • api/student/GetStudentById/{id} api/学生/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?fname=xxx (lname 将是null )
  • api/student/GetStudent/IdHere?lname=xxx (fname will be null ) api/student/GetStudent/IdHere?lname=xxx (fname 将是null )

(btw. I'm not sure why you pass the name parts, if id is required) (顺便说一句。如果需要id ,我不确定你为什么要传递名称部分)

I suppose you should call your API with below format:我想您应该使用以下格式调用您的 API:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM