Web API with Delete method in ASP Net MVC 5 Part | 8

preview_player
Показать описание
Here You can learn, how to implement WEB api with HTTP delete method in
ASP Net MVC 5

HTTP DELETE:
The HTTP DELETE request is used to delete an existing record in the database.

We use here Attribute Routing
1) [RoutePrefix("api/employee")] in top of controller

2) [Route("del")] on top of action method where you want to access using this.

Now let us start the creation of web API Delete Method WITH THE help of ASP.NET MVC:

CREATE TABLE [employees]
(
[empid] [bigint] NOT NULL,
[Name] [nvarchar](500) NULL,
[salary] [float] NULL,
PRIMARY KEY CLUSTERED ([empid] ASC)
)

Plan to delete the record of an employee where empid is 101

[Route("del/{empid}")]
[HttpDelete]
public IHttpActionResult delEmployee(int empid)
{
if(empid put here less sign 0)
{
return BadRequest();
}
SampleEntities db = new SampleEntities();
if(result!=null)
{
db.Entry(result).State = System.Data.Entity.EntityState.Deleted;
db.SaveChanges();
}
else
{
return BadRequest("emp id is not valid");
}
return Ok();
}
Рекомендации по теме
join shbcf.ru