Wednesday, August 14, 2019

Creating a Delete method in Asp.Net Core WebAPI Controller

In this post we will create a HttpDelete method in Asp.Net Core WebAPI, which will be used to delete a row in the SQL Server database using Entity Framework Core. We have seen how to setup Entity Framework Core in an Asp.Net Core Web API project in the previous posts, in this post we will jump into creating the HttpDelete method.

Open the Country controller which we used in the previous post and add the following Delete method which uses the HttpDelete verb.
        [HttpDelete]
        public void Delete(int countryId)
        {
            var _context = new UserRegistrationContext();
            var country = _context.Country.Find(countryId);
            _context.Country.Remove(country);
            _context.SaveChanges();

        }

Once the method is writer build and run the project, we can invoke the Delete method using Postman as follows.




This will delete the value row with id=6 in the Country table in the SQL server database, you can verify this by running the GET method again which will return the updated list as follows.




Search Flipkart Products:
Flipkart.com

No comments: