我在尝试从Windows窗体应用程序向WebApi发送带有7.8
等十进制值的WebClient请求时遇到问题。当我尝试在WebApi的Controller上访问此值时,此值为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
以下是我对Controller的行动:
<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
请注意,数量也是小数,并且发送正确
请求帮助人员。
答案 0 :(得分:0)
我弄清楚问题是什么。我在CDec(7.8).ToString().Replace(",", ".")
上添加的值在运行时被解析为字符串,因此当添加十进制值时,值将更改为&#34; 7,8&#34;逗号是问题所在。对于测试我只做toggleFullscreen()
并且它有效。所以现在我正在改变另一种方式来正确发送数据。