The webapi post method with parameters does not work

When I call my webAPI controller, which contains the post method with NO parameters, it jumps to the method. However, when I pass the parameters (and when I update the api controller with the parameters), see the Snippet below the first snippet. I get a 405 error that does not support POST.

var captchURL = "/api/Captcha"; $.ajax({ url: captchURL, dataType: 'json', contentType: 'application/json', type: 'POST' }) var jsondata = {solution: "7", answer: "7"}; var captchURL = "/api/Captcha"; $.ajax({ url: captchURL, dataType: 'json', contentType: 'application/json', type: 'POST', data: JSON.stringify(jsondata) }) 

UPDATE - Controller code:

 public class CaptchaController : ApiController { private readonly ICaptchaService _service; public CaptchaController(ICaptchaService service) { _service = service; } public Captcha Get() { return _service.Get(); } [HttpPost] public bool Post(string solution, string answer) { return _service.Post(); } } 

UPDATE - WebApiConfig:

  config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 

Is it because I do not have solution and response parameters (in my WebApiConfig), that it does not recognize them?

What am I doing wrong?

+5
source share
7 answers

This is a slightly different way to configure the route, but I prefer it. In the controller code, add the route prefix and routes for each method that will represent the POST of the GET request ...

 [RoutPrefix("Captcha")] public class CaptchaController : ApiController { [Route("Post/{solution}/{answer}")] [HttpPost] public bool Post(string solution, string answer) { return _service.Post(); } } 

This should work as long as you set the path correctly using correctly typed parameters and return a correctly entered value. If you are using a model, you do not need to add parameters to the route path.

This worked for me when I configured my WebAPI. If anyone sees something wrong with an explanation, please let me know. I am still participating (and always will be), but I just wanted to tell you what I did, what worked.

+6
source

create model

 public class Captcha { public string solution { get; set; } public string answer { get; set; } } 

the controller is

 [HttpPost] public string Post([FromBody] Captcha cap) { return cap.solution + " - " + cap.answer; } 
+4
source

Add another MapHttpRoute that will take the “ decision ” and “ answer ” as parameters

:

 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{solution}/{answer}", defaults: new { solution = RouteParameter.Optional, answer= RouteParameter.Optional } ); 
+3
source
 [HttpPost] public bool Post(Captcha cap) { return _service.Post(); } 

and

Change data: JSON.stringify(jsondata) to data : jsondata .

+2
source

The reason why it does not work is that there is no route that will take 2 parameters to your [post] address - api / captcha /.

you have 2 options, or configure attribute routing in the controller as

 [Route("Post/{solution}/{answer}")] [HttpPost] public bool Post(string solution, string answer) { return _service.Post(); } 

or create a model like

 Public class CaptchaModel { Public string Solution {get; set;} Public string Answer {get; set;} } 

And in your action method

 [HttpPost] public bool Post([FromBody] CaptchaModel model) { var solution = model.solution; var answer = model.answer; ......... return _service.Post(); } 
+1
source

According to www.asp.net,

"when the HTTP method is configured to be used on the server, but it has been disabled for this URI, the server will respond with an HTTP 405 Not Allowed error."

Further link to 405

http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications

+1
source

You must remove JSON.stringify (data) from your request, and then add [FromBody] in front of your model. Then it will work.

+1
source

Source: https://habr.com/ru/post/1239094/


All Articles