vue.js Http 405 ASP.NET核心终结点上不允许使用方法

lkaoscv7  于 2023-01-02  发布在  Vue.js
关注(0)|答案(2)|浏览(136)

Vs.js代码-这里是axios.delete和挂载的部分:

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

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

C#代码-这是后端部分(控制器):

[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);
}

我想我在vue.js axios.delete部分犯了一个错误。我得到了错误代码405。我该怎么修复它呢?

gcuhipw9

gcuhipw91#

你该换衣服了

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

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

并在C#API代码中执行此操作:

[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);
}

或者,如果您使用id的查询字符串调用Delete API,则应在api中设置FromUri
查看此link

cx6n0qe3

cx6n0qe32#

Abbas Aryanpour指出代码中缺少/是正确的,但是我认为错误消息应该是404,而不是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.

    • 正式文件:**

1.解决连接错误
2.跨源资源共享

相关问题