public IEnumerable<Product> GetProductsByCategory(string category)
{
{
return repository.GetAll().Where(
return repository.GetAll().Where(
p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));
p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));
}
}
//Post
[ValidateModel]
//Response code: By default, the Web API framework sets the response status code to 200 (OK).
//But according to the HTTP/1.1 protocol, when a POST request results in the creation of a resource, the server should reply with status 201 (Created).
//Location: When the server creates a resource, it should include the URI of the new resource in the Location header of the response.
[HttpPost]
[HttpPost]
[Route("api/v2/products")]
[Route("api/v3/products")]
public HttpResponseMessage PostProduct(Product item) //create product
public HttpResponseMessage PostProduct(Product item)
{
{
if (ModelState.IsValid)
if (ModelState.IsValid)
{
{
item = repository.Add(item);
item = repository.Add(item);
var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);
var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);
string uri = Url.Link("getProductById", new { id = item.Id });
// Generate a link to the new product and set the Location header in the response.
string uri = Url.Link("getProductByIdv3", new { id = item.Id });