【问题标题】:405 Method Not Supported on HTTP Delete with .Net Core 3.0 WebApi.Net Core 3.0 WebApi 的 HTTP 删除不支持 405 方法
【发布时间】: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


【解决方案1】:

对于[HttpDelete] on action,如果您使用GET 方法调用它,您将收到405 错误。您需要将您的测试方式分享给我们。在 Postman 中,405 来自

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

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 2016-11-18
    • 2017-09-12
    • 1970-01-01
    • 2011-07-19
    • 2018-04-03
    • 2014-06-14
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多