Jacek
Jacek

Reputation: 12053

ApiController: missing location in response

In my API project when resource was created, there is running this code

public class MyCtrl : ApiController
{
    ...
    if (isSuccess)
    {
        result.Id = newResource.Id;
        var locationUrl =  Request.RequestUri + "/" + id;

        return Created(locationUrl, result);
    }
}

But in response I see only id, how can force WebAPI to also return locationId?

{
    "id": "YHEMPZIF2VHHP6X7"
}

Upvotes: 0

Views: 363

Answers (2)

Michał Komorowski
Michał Komorowski

Reputation: 6238

The first parameter of Created method is passed to the client side in the response headers. More precisely in the Location header. So in order to access it you don't have to modify the result object.

enter image description here

As to accessing headers on the client side. For example in ASP.NET MVC you can do it via Request.Headers and in Angular via headers property of a response object.

Upvotes: 2

Seabizkit
Seabizkit

Reputation: 2415

like

if (isSuccess)
{
    var locationUrl =  Request.RequestUri + "/" + id;
    var result = new { Id:newResource.Id, locationUrl: locationUrl   }
    return Created(locationUrl, result);
}

Upvotes: 2

Related Questions