Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple controller which takes POST is not found

I've made some previous question asking for the help with the problems since I updated MVC4 webapi beta to RC. I got most in order now, but here's one I cannot figure out the reason for yet.

For this simple controller I have one which accepts a POST and one that accepts GET. When I try to run those by sending request from a HTML form, only the GET controller is found while the POST one will return me the following error.

{   "Message": "No HTTP resource was found that matches the request URI 'http://localhost/webapi/api/play/test'.",   "MessageDetail": "No action was found on the controller 'Play' that matches the name 'test'." } 

Why is the POST controller not found?

Controllers

public class PlayController : ApiController {     [HttpPost]  // not found     public string Test(string output)     {         return output;     }      [HttpGet]  // works     public string Test2(string output)     {         return output;     } } 

HTML form

<form action="http://localhost/webapi/api/play/test" method="post"> <input type="text" name="output" /> <input type="submit" name="submit" /> </form>  <form action="http://localhost/webapi/api/play/test2" method="get"> <input type="text" name="output" /> <input type="submit" name="submit" /> </form> 
like image 807
starcorn Avatar asked Sep 02 '12 15:09

starcorn


People also ask

How do you get POST data on a controller?

you can simply get all post data using $this->input->post() and $_POST using simple php stuff.

What is POST and get method in MVC?

Both GET and POST method is used to transfer data from client to server in HTTP protocol but the Main difference between the POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from ...

What is the default action for a controller?

By default, the Index() method is a default action method for any controller, as per configured default root, as shown below. routes. MapRoute( name: "Default", url: "{controller}/{action}/{id}/{name}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.


1 Answers

Web.API is a little bit picky when you want to post "simple" values.

You need to use the [FromBody] attribute to signal that the value is not coming from the URL but from the posted data:

[HttpPost] public string Test([FromBody] string output) {     return output; } 

With this change you won't get 404 anymore but output will be always null, because Web.Api requries the posted values in special format (look for the Sending Simple Types section):

Second, the client needs to send the value with the following format:

=value

Specifically, the name portion of the name/value pair must be empty for a simple type. Not >all browsers support this for HTML forms, but you create this format in script...

So recommend that you should create a model type:

public class MyModel {     public string Output { get; set; } }  [HttpPost] public string Test(MyModel model) {     return model.Output; } 

Then it will work with your sample froms without modifing your views.

like image 60
nemesv Avatar answered Oct 05 '22 08:10

nemesv