aspnet webapi将查询字符串绑定到对象

时间:2012-09-07 16:22:58

标签: asp.net asp.net-web-api model-binding

<HttpGet()>
Public Function Search(<FromUri()> ByVal name As Name) As HttpResponseMessage
  // get params from complex type
  // or check for model validation
  name.firstName;
  name.lastName;
End Function

Public Class Name
 <Required()>
 Public firstName As String
 <Required()>
 Public lastName As String
End Class
  

/ API / ABC /搜索的firstName =插孔&安培; lastName的=丹尼尔斯

我正在尝试将comlex类型作为查询参数发送,但即使我使用fromUri属性,name也始终为null。我错过了什么?

编辑:我也在使用System.ComponentModel.DataAnnotations中的Required()属性。

1 个答案:

答案 0 :(得分:1)

我发现了问题。我错过了我的字段中的Property关键字。

Public Class Name
 <Required()>
 Public firstName As String
 <Required()>
 Public lastName As String
End Class

因此,它适用于以下变化。

Public Class Name
 <Required()>
 Public Property firstName As String
 <Required()>
 Public Property lastName As String
End Class

问题解决了。我认为,没有属性,类不会暴露它的字段,所以我不能从uri中读取它们。