How to use Delete verb in WEB-API in MVC 4.0?


CRUD request is processed with the help of four methods of WEB-API which are GET,POST, PUT, DELETE. Client is not aware from which layer the request is coming. If you are new to WEB-API in MVC 4.0 then this code will guide you. Please post your technical queries here incase if you have. It would be great to help you.

Why to use HTTP Restful ?
1)We can seperate concerns which are associated with client as well as server.
2)Stateless connections are used which contains all the information needed to process the request.
3)Cacheable responses are generated.
Concept description -
HTTP verbs are used to map the methods that are called in response to HTTP requests. Web API has many advantages. It can be tested individually by using web debugging tool for e.g.Fiddler.
Following code is used for delete records.

   
public HttpResponseMessage Delete(int id)
{
yourtablename tblobject = db.yourtablename.Find(id);

if (tblobject == null)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound,"Error found.");
}

db.yourtablename.Remove(tblobject);

try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound,"Error found.");
}

return Request.CreateResponse(HttpStatusCode.OK, tblobject);
}

You can host your application in IIS and test in fiddler.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: