Web API 2, which is the best way to return an empty body for 201 responses

In the POST Web API 2 method, it seems like the best way to respond is to use the CreatedAtRoute method . However, in some answers I would like the body of the answer to be empty. So the answer is only with 201 and the location header.

I expect this to work:

[HttpPost]
[ResponseType(typeof(Product))]
public IHttpActionResult Post([FromBody] Product product)
{
    products.Add(product);
    return CreatedAtRoute<Product>("", new {id = product.Id}, null);
}

However, by checking this in Postman, it actually returns the string "null". Is there an elegant way to do this?

+4
source share
1 answer

You can use the built-in "Created" method for simplicity, but you said that the response type should be "Product".

Created("getPathHere", newProductObject);

, ( , , ).

Created(string.Format("/api/Products/{0}", productId), new { ProductId = productId, Status = "Awaiting Manufacture" });

0

Source: https://habr.com/ru/post/1668504/


All Articles