0

I'm getting a 405 error when trying to Delete from any Controllers. I can do it just fine in VS IDE, but cannot figure out why Not in host (request will sent by ajax)

Header Controller

[Authorize]
[Route("Api/v{version:apiVersion}/Cart")]
[ApiVersion("1.0")]
[ApiController]
public class CartApiController : ControllerBase

Note that: tried with CartApiController : Controller too

Controller

[HttpDelete("{CartId:long}", Name = "DeleteCart")]
public IActionResult DeleteCart(long CartId)
{
            if (!_CartRepository.DeleteCart(CartId,Convert.ToInt64(_AccountRepository.GetClaim("ID"))))
                return Ok(_ResultContentRepository.GetResultContent(1));

            return Ok(_ResultContentRepository.GetResultContent(200));
 }

Sender

SendApiAsync("Api/Cart/" + input.id, "delete", null, true, false, false).then(function () {
                location.reload();
});

Part of Ajax

$.ajax({
        url: Url,
        headers: Headers,
        type: Type,
        async: true,
        data: Data,
        contentType: "application/json",
        dataType: 'json',
        success: function (data) {

            etc...

        }

note that:

  1. this Api Sender Works fine by all Methods except http delete only on host

  2. didn't wrote the complete code for Api sender

  3. if url has a api address, url will be replace with the right path (it's not a path problem)

Alien 1377
  • 23
  • 5
  • 1
    If you are using iis on your server and that is where you are having a problem, you can check this post for help: [how-do-i-enable-http-put-and-delete-for-asp-net-mvc-in-iis](https://stackoverflow.com/questions/12440277/how-do-i-enable-http-put-and-delete-for-asp-net-mvc-in-iis) – Ryan Wilson Apr 27 '22 at 15:22
  • That linked question isn't relevant for ASP.NET Core. – Martin Costello Apr 27 '22 at 15:29
  • @MartinCostello so what is it then... – Alien 1377 Apr 27 '22 at 16:23
  • @RyanWilson its just a plesk panel, should i sent Ticket? (for the first answer, none of them checked as the answer realy ): ) – – Alien 1377 Apr 27 '22 at 16:23
  • 1
    Why is it not relevant? If the application is hosted in iis on the server, I would say it is relevant. - [asp-net-core-with-iis-http-verb-not-allowed](https://stackoverflow.com/questions/48188895/asp-net-core-with-iis-http-verb-not-allowed) – Ryan Wilson Apr 27 '22 at 16:37
  • @RyanWilson yes i read this article: [link](https://support.plesk.com/hc/en-us/articles/115002202793-Unable-to-use-DELETE-PUT-or-PATCH-HTTP-verbs-with-php-files-or-Web-API-on-a-Plesk-for-Windows-server-405-HTTP-verb-used-to-access-this-page-is-not-allowed) maybe it is. waiting for an answer from host support – Alien 1377 Apr 27 '22 at 17:06
  • @RyanWilson I've never had to configure that for any ASP.NET Core application running in IIS on Windows, and I've never seen any documentation for it saying it is. All requests are routed through to dotnet by the ASP.NET Core Module for IIS. – Martin Costello Apr 28 '22 at 09:11
  • @MartinCostello Just because you've never had to do it doesn't mean it isn't relevant. Perhaps someone has configured your server to allow these types of HTTP Verbs. WebDAV seems to be an issue for some. Even in the linked article above - [asp-net-core-with-iis-http-verb-not-allowed](https://stackoverflow.com/questions/48188895/asp-net-core-with-iis-http-verb-not-allowed) for .net core 2 and another user had the same issue using .net 5. The fact that the OP is getting 405 errors sure seems like that is the issue. – Ryan Wilson Apr 28 '22 at 13:13

1 Answers1

0

I've Asked About this issue with the support team of my website Hosting and they told me it's a security configuration I can't use Http Delete so I Changed Every Http Delete to Http Get Starting With "Delete/" Path

Alien 1377
  • 23
  • 5
  • It's not a good idea to use GET as a delete request. if you can't use DELETE then POST would be ideal. https://stackoverflow.com/questions/786070/why-should-you-delete-using-an-http-post-or-delete-rather-than-get – Swrena Feb 26 '23 at 21:01