0

I'm building an ASP.NET Core 5.0 Web API application as I mentioned in the title I have an issue when trying to delete a record from the database; I'm getting an error 405 Method Not Allowed response from HttpDelete request.

PS: I have added services.AddCors() and app.UseCors() with default policy.

This is the delete method code

public bool deleteLivreById(int id)
{
    Livre l = _db.Livres.Find(id);
    _db.Livres.Remove(l);
    _db.SaveChanges();
    return true;
}

And this is the HttpDelete method inside the controller

[HttpDelete("{id}/delete")]
   public bool deleteLivreById(int id)
      {
        return  _objGererLivre.deleteLivreById(id);
      }

Finally this is a picture from console when navigating to HttpDelete Url

enter image description here

Edit: This is full code of my controller

namespace GestionLivre.Controllers
{
    [ApiController]
    [Route("test")]
    public class LivreController : Controller
    {
        private IGererLivre _objGererLivre;
        public LivreController(IGererLivre gererLivre)
        {
            _objGererLivre = gererLivre;
        }
        [HttpGet]
        public JsonResult getLivres()
        {
            return Json(_objGererLivre.getLivres());
        }
        [HttpDelete("{id}/delete")]
            public bool deleteLivreById(int id)
        {
               return  _objGererLivre.deleteLivreById(id);
        }
}
}
3
  • Could you show your controller route? Commented Dec 29, 2021 at 14:29
  • Also you probably should add [FromRoute] attribute for : public bool getById([FromRoute]int id) Commented Dec 29, 2021 at 14:32
  • I just edited the post and added full controller code also i have tried to add [FromRoue] but still got the same problem Commented Dec 29, 2021 at 14:40

2 Answers 2

3

I opened the screenshot and noticed that you have selected 'GET' as http verb and method type is 'Delete'. Could you please change that and try.

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

1 Comment

I noticed that but i didn't really know how to change the header request 'GET' while the controller i'm giving a 'HttpDelete'. If you could point me on how to change it
1

As I understand by default when you're trying to access URL in browser it uses GET method. So we should to pass in header appropriate method(POST,GET,DELETE,PATCH,PUT) If you want to test HTTP methods I'll recommend you to use Postman or Swagger. Postman much easier to use whether than Swagger which you should to add to service configuration and middleware. Example of Postman: enter image description here

And than configure body like that to return response. enter image description here

Also recommend you to use REST Best Practices. And name resources properly. https://restfulapi.net/resource-naming/#:~:text=2.-,Best%20Practices,-2.1.%20Use%20nouns

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.