0

I have a controller like this:

[ApiController]
[Route("[controller]")]
public class EmployeeController : ControllerBase
{
    [HttpGet("[action]/")]
    public async Task<IActionResult> GetAll()
    {
        return Ok(await _employeeService.GetAllEmployees());
    }
    [HttpDelete("[action]/{id}")]
    public async Task<IActionResult> Delete(int id)
    {
        await _employeeService.DeleteEmployee(id);
        return Ok();
    }
}

Through Swagger, everything works great, all requests are detected. But if you enter a direct request, in my case: https://localhost:7080/Employee/Delete/3, then 405 is returned. I read on the Internet, I met a situation, only with deployed APIs, but it doesn’t even work for me on a local host. What could be missing?

EDIT Also trying to use it in TypeScript like this:

async function deleteById(url: string, id: number) {
    return await fetch(url + id.toString(), { method: 'DELETE', }).then((response) => {
        if (!response.ok)
            console.log(response);
        else
            return response.json();
    });
}
deleteById("https://localhost:7080/Employee/Delete/", id).then((json) => {
        console.log(json);
    });

And here is what i get... enter image description here

aquaprogit
  • 103
  • 4
  • 4
    By direct request, do you mean just entering "https://localhost:7080/Employee/Delete/3" in a browser? Because that will still just make a GET request. To really test PUT, DELETE, etc you should use a tool like Postman. – Valuator Aug 15 '22 at 13:55
  • 1
    where are you entering this request? in the browser? you need something like postman, browser will send a get request to your method resulting in the 405 – Ric Aug 15 '22 at 13:55
  • im using both browser and web page with typescript and sending request via fetch function – aquaprogit Aug 15 '22 at 13:56
  • Your backend code is fine. it's the way you're using the fetch API. See https://stackoverflow.com/questions/40284338/javascript-fetch-delete-and-put-requests – Bron Davies Aug 15 '22 at 14:31
  • 1
    In your screenshot you can see in the red line first word it say is "GET". You are sending a GET http request instead of a DELETE http request. – Dimitris Maragkos Aug 15 '22 at 17:49

1 Answers1

0

This worked for me -

<system.webServer>
  <handlers>
    <remove name="WebDAV" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
  <modules>
    <remove name="WebDAVModule" />
  </modules>
</system.webServer>

Also you can check this Link

artista_14
  • 674
  • 8
  • 22