webapi post method with parameters doesn't work

14,390

Solution 1

This is a slightly different method of setting the route, but I prefer it. In your controller code add a route prefix and routes for each method that will represent a POST of 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 are setting the path correctly, using the correctly typed parameters, and returning a correctly typed value. If you use a Model then you do not have to add the parameters into the Route path.

This worked for me when I was setting up my WebAPI. If anyone sees anything wrong with explanation please let me know. I am still learning (and always will be), but just wanted to let you know what I did that worked.

Solution 2

create a model

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

the controller is this

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

Solution 3

Add another MapHttpRoute which will accept 'solution' and 'answer' as parameters

sample :

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

Solution 4

[HttpPost]

public bool Post(Captcha cap)
{
    return _service.Post();
}

And

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

Solution 5

According to www.asp.net,

"when an HTTP method is configured for use on the server, but it has been disabled for a given URI, the server will respond with an HTTP 405 Method Not Allowed error."

Further reference on 405

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

Share:
14,390
webdad3
Author by

webdad3

Been programming for a while, and I can honestly say I've embraced my inner nerdiness. By doing so that has helped my programming skills as I work on a side project almost every night after coding for my job. I really think mobile application development is where the future of the industry is going. Most of my side projects are now related to the Windows Phone 8 and Windows 8. However, I do all my web work using PHP and MVC. I try to follow the mantra of keep it simple stupid! I'm focusing on networking and becoming part of the community of developers. Not only to help myself, but also to help others. Wisdom is earned through many failures (big and small). You can also follow me on Twitter!

Updated on June 04, 2022

Comments

  • webdad3
    webdad3 almost 2 years

    When I call my webAPI controller that contains a post method with NO parameters it goes to the method. However, when I pass parameters (and when I update the api controller with paramters as well) into this see the snippet below the 1st snippet I get the 405 error that it doesn't 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 don't have the solution and answer params (in my WebApiConfig) that it doesn't recognize them?

    What am I doing incorrectly?

  • webdad3
    webdad3 over 8 years
    When I did this I got into the controller but "cap" was null.
  • Mauro Sala
    Mauro Sala over 8 years
    are you sure your uri is correct? try to do this var captchURL = "api/captcha";
  • webdad3
    webdad3 over 8 years
    positive. I changed the captchURL as well with no change.
  • Mauro Sala
    Mauro Sala over 8 years
    try to remove private readonly ICaptchaService _service; public CaptchaController(ICaptchaService service) { _service = service; }