0

I found many open-source projects which do not use HttpPut and HttpDelete for updating and deleting. Instead, they add the HttpPost endpoint before their methods, and the functionalities are implemented successfully as well.

What I learned is that we use HttpPut and HttpDelete to update and delete. I am confused.

I have added examples of update methods and delete methods below.

UPDATE:

        [HttpPost]
        public async Task<IActionResult> Edit(int id, NewInvoiceVM invoice)
        {
            var allInvoices = await _service.GetAllAsync();

            var invoicesValidate = allInvoices.ToList().FirstOrDefault(n => n.SupplierName == invoice.SupplierName && n.InvoiceNumber == invoice.InvoiceNumber);
            await _service.UpdateInvoiceAsync(invoice);
            return RedirectToAction(nameof(Index));
        }

DELETE:

        [HttpPost, ActionName("Delete")]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var invoiceDetails = await _service.GetInvoiceByIdAsync(id);
            if (invoiceDetails == null) return View("NotFound");

            await _service.DeleteAsync(id);
            return RedirectToAction(nameof(Index));
        }

Question: Are HttpPut and HttpDelete required for Update and Delete method? If not, when should we use them?

4
  • 2
    What is your question? And who advised you to use only httpput or httpdelete? must be your school teacher. I never used them for all my life. Commented Feb 27, 2022 at 20:07
  • @Serge Just confused why HttpPut and HttpDelete are not used for Update and Delete methods. Commented Feb 27, 2022 at 21:06
  • All of them are working the same way, maybe Get is a different since it doesn have body. Just don't assign any methods at your APIs at all. Let users decide what to use. Commented Feb 27, 2022 at 21:11
  • The reason you should use the HttpDelete and HttpPut annotations is because it's good RESTful symantics. It's not necessary, because in some cases you'll want to include some data in the body of the request, which you cannot do with HttpDelete, for example. In general, it's good practice to use the correct annotations, though. Commented Feb 27, 2022 at 21:12

3 Answers 3

2

If you're implementing an API you have many ways to do it. Using PUT, DELETE, UPDATE etc are all recommended best practices, but there's no need to use them.

You can write an API that listens for a DELETE request to delete something or you can write an API that listens for a POST request with another parameter that tells it if it's a delete, or something else. Both work just fine.

The reason you do it a certain way, is so that when other people come to use your stuff, it will be intuitive for them, if you've used the best practices recommendation. This goes for PUT/POST as it does for all coding best practices.

Sign up to request clarification or add additional context in comments.

Comments

1

Historically, web browsers did not have native support for PUT, PATCH, DELETE, and other verbs. They only support GET and POST. API's that implemented PUT etc. had to do it by sending a POST with a hidden field (often named _method) to pretend that they were sending something other than a POST.

For this reason, many projects just didn't think it was worth implementing.

Comments

0

There is already a example how Microsoft does it. This is the way you should do it in most cases (seems you are using .Net?).

https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio#the-deletetodoitem-method

Maybe the people in the repositories you saw just made mistakes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.