I think this is what you should do
[HttpDelete]
public HttpResponseMessage DeleteCart(int id)
{
//not sure what it does?
if (IsNetworkAvailable(0))
{
var del = db.Carts.First(e => e.Id == id);
db.Carts.Remove(del);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, "Delete Successfully");
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "No Network");
}
}
you can also think about using try/catch blocks when saving the changes after deletion to handle unexpected exceptions and return proper message back to the client.
You should avoid making redundant calls to the database in your backend. I believe that if you only want to remove an item from a given table, you should only send id of this item. If you have a problem to deliver the id of the item then there may be a problem with the implementation of the client side.
Edit
i want to achieve that when ever i give a user id it will delete
complete cart data of that user
[HttpDelete]
public HttpResponseMessage DeleteUserCarts(int userId)
{
//not sure what it does?
if (IsNetworkAvailable(0))
{
var carts = db.Carts.Where(e => e.UID == userId);
db.Carts.RemoveRange(carts);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, "Delete Successfully");
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "No Network");
}
}