1

I got an Service to Post Data and Returning Location of Created Object

 public IHttpActionResult Post([FromBody]ErbrachteLeistung value)
{
  int newLeistungId = service.AddErbrachteLeistung(value);
  return Created($"api/ErbrachteLeistung/{newLeistungId}", value);
}

My Client Looks like this

HttpClientHandler handler = new HttpClientHandler
  {
    UseDefaultCredentials = true,
    AllowAutoRedirect = true
  };

  using (HttpClient client = new HttpClient(handler))
  {
    client.BaseAddress = new Uri(WebApiUri);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = client.PostAsJsonAsync<ErbrachteLeistung>("api/ErbrachteLeistung", erbrachteLeistung).Result;
    response.EnsureSuccessStatusCode();
  }

How can I read the Created Location given in the WebService out of the Response?

Sebastian Mehler
  • 418
  • 6
  • 15

1 Answers1

0

The 201 Created response stores that value in the Location Header

//...
response.EnsureSuccessStatusCode();
Uri location = response.Headers.Location;

//...
Nkosi
  • 235,767
  • 35
  • 427
  • 472