I am wondering how to get the id of inserted record returned in http response to POST method
Controller method:
public HttpResponseMessage POST([FromBody]Map newmap)
{
try
{
using (SHATDbEntities entities = new SHATDbEntities())
{
entities.Maps.Add(newmap);
entities.SaveChanges();
var message = Request.CreateResponse(HttpStatusCode.Created, newmap);
message.Headers.Location = new Uri(Request.RequestUri + newmap.Id.ToString());
return message;
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
Response Handled:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:2351/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsJsonAsync(RequestURI, newmap).Result;
int mapid = int.Parse(responsestatusCode.Content.ReadAsStringAsync().Result)
Explanation:
I tried other solution which available on stack overflow but didn't get solution and above code but it through an exception i.e here