Demo entry 6863706

123

   

Submitted by anonymous on Aug 13, 2019 at 04:24
Language: C#. Code size: 1.3 kB.

[HttpDelete]  
[Route("customers/{id:int}")]  
public IHttpActionResult DeleteCustomer(int id)  
{  
    try  
    {  
        // Find the customer to be deleted in the repository  
        var customerToDelete = repository.GetCustomer(id);  
  
        // If there is no such customer, return an error response  
        // with status code 404 (Not Found)  
        if (customerToDelete == null)  
        {  
                return NotFound();  
        }  
  
        // Remove the customer from the repository  
        // The DeleteCustomer method returns true if the customer  
        // was successfully deleted  
        if (repository.DeleteCustomer(id))  
        {  
            // Return a response message with status code 204 (No Content)  
            // To indicate that the operation was successful  
            return StatusCode(HttpStatusCode.NoContent);  
        }  
        else  
        {  
            // Otherwise return a 400 (Bad Request) error response  
            return BadRequest(Strings.CustomerNotDeleted);  
        }  
    }  
    catch  
    {  
        // If an uncaught exception occurs, return an error response  
        // with status code 500 (Internal Server Error)  
        return InternalServerError();  
    }  
}  

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).