Home > other >  Http 405 MethodNotAllowed on ASP.NET Core endpoint
Http 405 MethodNotAllowed on ASP.NET Core endpoint

Time:01-02

Vs.js code - here are axios.delete and mounted sections:

mounted() {    
    this.getCreated();
},

deleteRequest(id) {
   axios.delete("http://localhost:32961/api/request/delete" id)
      .then(() => {
        this.getCreated()
      })
      .catch((err) => console.error(err));
},

C# code - this is the backend part (controller):

[Route("delete/{id}")]
[HttpDelete]
public IActionResult Delete([FromBody] Request request)
{    
    _db_Context.Requests
               .FirstOrDefault(a => a.RequestId == request.RequestId);

    _db_Context.Requests.Remove(request);
    _db_Context.SaveChanges();

    return Ok(request);
}

I think I'm making a mistake in the vue.js axios.delete part. I am getting error code 405. How can I fix it?

CodePudding user response:

You should change

axios.delete("http://localhost:32961/api/request/delete" id)

to

axios.delete("http://localhost:32961/api/request/delete/" id)

And do this in the C# API code:

[Route("delete/{id}")]
[HttpDelete]
public IActionResult Delete(int id)
{    
    _db_Context.Requests
               .FirstOrDefault(a => a.RequestId == request.RequestId);

    _db_Context.Requests.Remove(request);
    _db_Context.SaveChanges();

    return Ok(request);
}

Or if you call Delete API with query string of id you should set FromUri in api.

See this link

CodePudding user response:

Abbas Aryanpour is right in pointing out the missing / in your code. But I think the error message should be 404, not 405.

I think this should be related to your asp.net core back-end code not setting cross-domain, or you have set cross-domain, but did not notice the execution order of middleware, so it may cause cross-domain not to take effect.

Official doc:

1.Troubleshoot connection errors

2. Cross-origin resource sharing

  • Related