【问题标题】:Error 405 not allowed on HttpPost in Asp.net core 3.1 web api在 Asp.net core 3.1 web api 中的 HttpPost 上不允许出现错误 405
【发布时间】:2021-11-11 15:41:06
【问题描述】:

当我尝试使用邮递员发布原始文本时,服务器回答 405 不允许。我尝试添加

app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());

services.AddCors();

没有任何解决办法。

这是应用程序的代码:

   [Route("api/[controller]")]
    [ApiController]
    public class VideoWallController : ControllerBase
    {
        // GET: api/<ValuesController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<ValuesController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<ValuesController>
        [HttpPost]
        public string prova([FromBody] VideoCallBack test)
        {
            return "true;";
        }

        [HttpPost]
        public void teststring(string test)
        {

        }

        // PUT api/<ValuesController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<ValuesController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }

【问题讨论】:

    标签: asp.net asp.net-core .net-core http-status-code-405


    【解决方案1】:

    首先,如maghazade所说,there are two endpoints for the post method。您可以尝试使用[HttpPost("teststring")],如ferhrosa所说。您也可以将[action]添加到[Route("api/[controller]")],这样动作的路线就会包括他们的动作名称。如果您使用 post 方法添加其他操作,路由将不再冲突。

    [Route("api/[controller]/[action]")]
        [ApiController]
        public class VideoWallController : ControllerBase
        {
            // GET: api/<ValuesController>/Get
            [HttpGet]
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET api/<ValuesController>/Get/5
            [HttpGet("{id}")]
            public string Get(int id)
            {
                return "value";
            }
    
            // POST api/<ValuesController>/prova
            [HttpPost]
            public string prova()
            {
                return "true;";
            }
            // POST api/<ValuesController>/teststring
            [HttpPost]
            public void teststring([FromBody]string test)
            {
    
            }
    
            // PUT api/<ValuesController>/Put/5
            [HttpPut("{id}")]
            public void Put(int id, [FromBody] string value)
            {
            }
    
            // DELETE api/<ValuesController>/Delete/5
            [HttpDelete("{id}")]
            public void Delete(int id)
            {
            }
        }
    

    如果你想从Body传递string test,你需要在postman中添加[FromBody]并选择JSON

    结果:

    【讨论】:

      【解决方案2】:

      如果你想使用/teststring 后缀到达那个端点,你需要把它放在端点模板中:

      [HttpPost("teststring")]
      public void teststring(string test)
      {
      }
      

      或者,正如@maghazade 所说,您可以使用控制器 URL 到达端点,不带后缀:https://localhost:44336/api/videowall

      此外,使用 Postman 访问 API 不需要 CORS 设置。 CORS 仅用于通过在 Web 浏览器上运行的 Web 应用程序配置 API 访问。

      【讨论】:

        【解决方案3】:

        根据你定义的路由,如果你删除teststring方法并调用这个地址,post方法有两个端点,你正在考虑的端点会被调用。重要的是url如下,否则会尝试调用get方法。这就是它返回 405 错误的原因

        GET https://localhost:44336/api/videowall/teststring

        POST https://localhost:44336/api/videowall/

        【讨论】:

          猜你喜欢
          • 2021-02-14
          • 2020-07-06
          • 2021-09-30
          • 2016-05-09
          • 2017-10-18
          • 2018-02-04
          • 2017-11-03
          • 2021-12-02
          • 2018-05-27
          相关资源
          最近更新 更多