How to delete a resource, i.e implement HTTP DELETE in ASP.NET Core REST API.
To delete a resource, issue an HTTP DELETE request to the URI /api/employees/ID. The ID of the employee to delete must be passed in the URI.
ASP.NET Core REST API - HTTP DELETE Example
Code Explanation
It is the DELETE request that is used to delete an existing employee. This is the reason DeleteEmployee() method is decorated with the HttpDelete attribute.
The id of the employee to delete is passed as a parameter to the HttpDelete attribute. This id will be appended to the URL /api/employees. So the URL becomes /api/employees/id
The employee id value in the URL is automatically mapped to the id parameter on the DeleteEmployee(int id) method.
As we are using the int route constraint on the id route parameter, the id value in the URL is mapped to the method parameter, only if the value is an integer.
ASP.NET Core Web API CRUD Operations Example
The following is the complete EmployeesController code. We have all the CRUD operations (i.e Create, Read, Update and Delete) implemented.
EmployeeRepository
EmployeeRepository uses entity framework core to store and retrieve data from SQL Server database.
No comments:
Post a Comment