Free Web Development Online Tutorials, Learn to Code
Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

Asp.net Web API Delete Method Example

Remember to check the authorization before processing the delete request, so you must check if the request has come from authenticated user

Get the tbClient object where clientId match id,if matching client found then delete

public HttpResponseMessage Delete(int id)            
{
try
{
    Client c = null;            
    // Get the tbClient object where clientId match id
    // if matching client found then delete            
    // else send a message back saying "no client found"
            
    var returnMessage = Request.CreateResponse(HttpStatusCode.Accepted, c);
    returnMessage.Headers.Location = new Uri(Request.RequestUri + "/" + c.ClientId.ToString());
    return returnMessage;

    }
catch (Exception ex)
{
    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}

Always keep the delete method type as HttpResponseMessage, so you can send customized response message back to user, so they know the end result of their call

Comment
Name
Email
Website
Subscribe
 
Asp.Net Web API Delete Example