Home > Software design >  Error 405 not allowed on HttpPost in Asp.net core 3.1 web api
Error 405 not allowed on HttpPost in Asp.net core 3.1 web api

Time:09-17

When i try to post the raw text with postman the server answer 405 not allowed. I try to add

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

services.AddCors();

without any solution.

Whis is the code of the application:

   [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)
        {
        }
    }

Error on postman

CodePudding user response:

If you want to reach that endpoint using the /teststring suffix, you need to put that in the endpoint template:

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

Or, as @maghazade said, you can just reach the endpoint using the controller URL, without a suffix: https://localhost:44336/api/videowall

Also, the CORS settings are not needed to access the API using Postman. CORS is used only to configure the API access through a web application running on a web browser.

CodePudding user response:

According to the routing you defined, there are two endpoints for the post method if you delete the teststring method and call this address The endpoint you are considering will be called. It is important that the url is as follows, otherwise it will try to call the get method. That's why it returns a 405 error

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

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

  • Related