【发布时间】:2020-03-28 19:02:26
【问题描述】:
我已经使用 .NetCore 3.0 生成了一个 WebAPI,但每次我尝试创建一个 HttpDelete 时它都会返回“405 Method not Supported”。
找了一会儿,我只发现“在 webconfig 中删除 WebDave”,但 .NetCore 3.0 WebApi 项目没有 WebConfig。
Edit2:该请求只是“https://localhost:5001/api/ambiente/1”上的删除请求,“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;
}
}
}
【问题讨论】:
-
控制器上是否有与请求的路由匹配的
[HttpDelete]属性的公共操作?如果不是,那就是问题 -
请分享相关控制器代码。
-
WebDAV 无关
-
举一个请求的例子。能否包含相关的控制器定义及其修饰属性(即 Route 等)
标签: asp.net-core http .net-core .net-core-3.0