0

I Post a new Waste entity using the following code:

var result = await httpClient.PostAsJsonAsync(wasteApiRoute, waste);

The Api Controller (using the code created by VS) seems to try to make life easy for me by sending back the new Id of the Waste entity using:

 return CreatedAtAction("GetWaste", new { id = waste.Id }, waste);

So the resultvariable wil contain this data. Indeed, I find it in its Headers.Location property as an url.

But how do I nicely extract the Id property from the result without resorting to regular expressions and the like? Surely the creators of ASP.Net Core will have included a nifty call for that?

Dabblernl
  • 15,831
  • 18
  • 96
  • 148

1 Answers1

0

Well, the best I can come up with is:

 var result = await httpClient.PostAsJsonAsync(wasteApiRoute, waste);
 var newWaste = await result.Content.ReadFromJsonAsync<Waste>();

Where waste has an Id of zero, newWaste has its Id set.

Dabblernl
  • 15,831
  • 18
  • 96
  • 148