NET Core 3.0 WebApi的HTTP删除不支持405方法

时间:2019-12-04 02:40:08

标签: .net http asp.net-core core .net-core-3.0

我已经用.NetCore 3.0生成了WebAPI,但是每次我尝试制作HttpDelete时,它都会返回“ 405不支持的方法”。

寻找了一段时间之后,我只发现“在webconfig中删除WebDave”,但是.NetCore 3.0 WebApi项目没有WebConfig。

Edit2:该请求只是对'https://localhost:5001/api/ambiente/1'的Delete请求,'https://localhost:5001/api/ambiente'的Get正常运行

控制器是通过命令dotnet aspnet-codegenerator controller -name AmbienteController -async -api -m Ambiente-dc CotacaoContext -outDir Controllers

生成的

和一个简化的(删除了其他方法)控制器的版本是:

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using CotacaoBackEnd.Data;
using CotacaoBackEnd.Models;

namespace CotacaoBackEnd.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AmbienteController : ControllerBase
    {
        private readonly CotacaoContext _context;

        public AmbienteController(CotacaoContext context)
        {
            _context = context;
        }


        // DELETE: api/Ambiente/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<Ambiente>> DeleteAmbiente(int id)
        {
            var ambiente = await _context.Ambientes.FindAsync(id);
            if (ambiente == null)
            {
                return NotFound();
            }

            _context.Ambientes.Remove(ambiente);
            await _context.SaveChangesAsync();

            return ambiente;
        }


    }
}

1 个答案:

答案 0 :(得分:0)

对于运行中的[HttpDelete],如果使用GET方法调用它,则会出现405错误。您需要以测试方式与我们分享。在邮递员中,405来自

enter image description here

如果您使用DELETE,它将得到200

enter image description here