Do you return a Resource URL?

Updated by Brady Stroud [SSW] 1 year ago. See history

123
<introEmbed body={<> When the Web API creates a resource, it should include the URI of the new resource in the Location header of the response. </>} />
public Product PostProduct(Product item)
{
item = repository.Add(item);
return item;
}

❌ Figure: Figure: Bad example – The response does not contain a reference to the location of the new resource

public HttpResponseMessage PostProduct(Product item)
{
item = repository.Add(item);
var response = Request.CreateResponse(HttpStatusCode.Created, item);
string uri = Url.Link("DefaultApi", new { id = item.Id });
response.Headers.Location = new Uri(uri);
return response;
}

✅ Figure: Figure: Good example – The response message contains a link in the header to the created resource (plus the “Created” status code is returned)


Acknowledgements

Adam Stephensen
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.