Mattias
Mattias

Reputation: 3179

How to get Id from ASP.Net web api CreatedAtRoute with jquery

I'm trying to learn how to get the id from the route that is created when creating a post in asp.net web api

[ResponseType(typeof(MyDTO))]
public IHttpActionResult PostmyObject(MyDTO myObject)
{
...
return CreatedAtRoute("DefaultApi", new { id = myObject.Id }, myObject);
}

That returns a 201 with the location but i now want to be able to get the id of that location and here is where i have no clue of how this could be done

$.ajax({
statusCode: {
201: function() {
  //what to do here?
 }
}
});

Upvotes: 0

Views: 816

Answers (1)

David Tansey
David Tansey

Reputation: 6023

You should be to retrieve the value of the location header with the following:

$.ajax({
  statusCode: {
    201: function(data, textStatus, xhr) {
      //what to do here?
      console.log(xhr.getResponseHeader('Location')); 
    }
  }
});

Upvotes: 1

Related Questions