Search code examples
c#.nethttpasp.net-web-apiasp.net-apicontroller

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"
}

Solution

  • 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.