skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. Firstly,as maghazade said,there are two endpoints for the post method.You can try to use [HttpPost("teststring")] as ferhrosa said.You can also add [action] to [Route("api/[controller]")],so that routes of actions will include their action names.If you add other actions with post method,the routes will not conflict anymore.

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

    And if you want to pass string test from Body,you need to add [FromBody] and select JSON in postman.
    enter image description here

    result:
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search