【问题标题】:Return 405 Method Not allowed from Asp.Net Core Webapi从 Asp.Net Core Webapi 返回 405 方法不允许
【发布时间】:2019-07-28 12:21:09
【问题描述】:

在下面的 API 控制器中,如果在 API 上调用 POST 或 PUT,如何返回 405 Method Not allowed。目前它正在返回 404 not found 。

[HttpGet]        
        public ActionResult<IEnumerable<Customer>> Get()
        {
            string query = "select* from Customers;";
            try
            {
                List<Customer> collection = GetCustomers(query);
                return Ok(collection);
            }
            catch (System.Exception)
            {
                return BadRequest();
            }
        }

【问题讨论】:

  • 你有一个[HttpGet] 属性。 .net core 会自动执行 405。
  • 如果您现在尝试使用 Post 或 Put 调用端点会发生什么?我相信它给出了 405
  • 返回 404 Not Found
  • GetCustomers 定义在哪里?
  • 那你的路线错了。即它实际上 404。如果您使用 GET 以外的任何其他方式执行该实际操作,那么您将得到 405。

标签: c# asp.net-web-api asp.net-core


【解决方案1】:

这个问题在这个线程中讨论: https://github.com/aspnet/Mvc/issues/388

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 2016-05-06
    • 1970-01-01
    • 2020-07-06
    • 2019-05-18
    • 2016-12-03
    • 2014-05-21
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多