Search code examples
c#ajaxiis-expresshttp-status-code-405asp.net-core-6.0

HTTP Error 405 When Using HTTP Delete Only on Host .Net Core 6


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)


Solution

  • 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