2017-01-25 26 views
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爲幫助傢伙。

回答

0

我弄清楚是什麼問題。我在NameValueCollection上添加的值在運行時被解析爲字符串,所以當添加十進制值時,值將變爲「7,8」,其中逗號是問題。對於測試,我只是做CDec(7.8).ToString().Replace(",", "."),它的工作原理。所以現在我正在改變另一種正確發送數據的方式。

Tks無論如何傢伙!