I have an Entity Framework object (code first) so it is essentially a POCO. I want to throw this over the wire to a WebAPI service (WebAPI is MVC).
- The target .net framework I am working in where I need to make the call is 4.0 (aspx) and this cannot be changed.
- The WebAPI that catches the call is 4.5.1, the latest MVC and EF versions but it's across the wire so there's no version conflicts.
- The EF class project that contains the objects I want to transmit is also 4.5.1 but I'm having no problems with it (having used it in the aspx project and it, miraculously, works without problem).
- Calling all EF methods and DBContext calls work flawlessly when embedded in the 4.0 project so this is not an issue either (we want to do this via web services, not direct EF calls).
- The EF code first models are being used only as POCO. For example, we use a List (of EF_Obj) to bind to DataGrids so, again, there is no version conflicts.
I have managed to get a simple GET call using the following:
Using client = New WebClient()
responseString = client.DownloadString(String.Format("http://localhost:1234/MyURLGet/{0}", txtValue.Text.Trim()))
myPOCO_Class = Newtonsoft.Json.JsonConvert.DeserializeObject(Of MyPOCO_EF_obj)(responseString)
End Using
This works great. The problem is trying to attach MyPOCO_EF_Obj
to the body of the POST when I want to insert into the db.
What I've found, so far, is this but it doesn't allow me to attach POCO classes, only strings:
Using client = New WebClient()
'assemble the data
Dim values As New NameValueCollection
values("value") = txtValue.Text.Trim()
values("description") = txtDescription.Text.Trim()
Dim response = client.UploadValues("http://localhost:1234/MyURLPOST/{0}", userName), values)
retVal = Encoding.[Default].GetString(response)
End Using
The problem with this code is that when it gets to the other side, values
is caught but none of the data goes with it.
public string Post(string username, [FromBody] System.Collections.Specialized.NameValueCollection values) { //etc }
I'd prefer to throw the POCO object over and deal with it there but sending the name/value pairs will work too.
That said:
Is it possible to do this with the POCO
[FromBody]
? If so, how?Why are my values not coming across to the POST handler?
Edit: for reference, I am currently looking at this SO question and response: