0
我在嘗試發送帶有十進制值的WebClient請求(如7.8
)到Windows窗體應用程序的WebApi時出現問題。當我試圖訪問WebApi的Controller上的這個值時,這個值是Nothing
。通過WebClient發送十進制值在控制器上得到Nothing
這裏是我的模型:
Public Class ProductModel
Public Property Name As String
Public Property Quantity As Decimal?
Public Property Price As Decimal?
End Class
這是在我的Windows窗體應用程序我請求功能:
Public Function SendProduct() As String
Using client As New WebClient
client.Headers.Clear()
client.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded"
client.Encoding = Text.Encoding.UTF8
Dim params As New Specialized.NameValueCollection
params.Add("Name", "Product 1")
params.Add("Quantity", CDec(7.0))
params.Add("Price", CDec(7.8))
Dim responseBytes As Byte() = client.UploadValues("http://localhost:50305/mycontroller/sendproduct", "POST", params)
Dim response As String = (New Text.UTF8Encoding).GetString(responseBytes)
Return response
End Using
End Function
這裏是我的控制器上的行動:
<HttpPost>
Public Function SendProduct(<FromBody> model As ProductModel) As IHttpActionResult
'At this point my model values are:
'model.Name = "Product 1"
'model.Quantity = 7
'model.Price = Nothing (Here is the problem)
End Function
注意數量也是十進制數,並且發送正確
Tks爲幫助傢伙。