我已经用.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;
}
}
}