6

Since a few days I'm trying to create my own web api controller. Duo to the rest conventions I need to use a post request to create an object. To get concrete, Im having this controller with this action:

public class ReservationController : ApiController
{
    [HttpPost]
    public void Create(int roomId, DateTime arrivalDate)
    {
      //do something with both parameters   
    }
}

This code is not working when I fire a post request at it, I'm receiving a 404 exception something like this:

No action was found on the controller 'Some' that matches the request.

The reason for it is that simple types are read from the query string, complex types from the body, according to this aricle. The web api uses the parameters to match the action to a request and can't therefore map my action to the request.

I do know that I can use the [frombody] tag, but you can only apply that to one parameter and I have 2. I also know that I can create a wrapper object which have both the parameters, but I'm not willing to use wrappers for all my calls.

So I do know that I can work around this by these methods. I also think that this is caused by the fact that the body of the post request can only be read once. But my actual question is:

Why is the source of a parameter determined by it's type and not by it's availability, especially when the conventions state that you should make for example a post request for creation? In MVC this is the case, why isn't it in the web api?

Best regards,

BHD

FINAL UPDATE Since I'm getting some upvotes, problably more people are facing the same question. In the end it comes to this: Web-Api != MVC. It's simply not the same thing and the web api team made different design decisions than the mvc team I guess.

3 Answers 3

4

It seems that you have a fundamental misunderstanding of how Web API actually works.

Web API routing is driven off of verbiage, not the method names. "SomeMethod" actually translates to zero useful information for Web API. As a result, if I post

api/some/some?id=1

OR

api/some/somemethod?id=1

OR EVEN

api/some/?id=1

and the SomeMethod endpoint is the ONLY available POST, it will hit that endpoint.

As such, first of all, make sure you have only one POST on that api controller. If you do, POSTing to it from any test client using either of the query strings above will work just fine.

3
  • If there were more than one POST method I think he would be getting a different error stating that more than one match was found. Not the error he is getting. Commented Nov 15, 2013 at 16:33
  • @TrevorElliott That is possible, but I also cannot reproduce his error locally. My guess is he is sending a call to a url that looks like api/some/somemethod/1 or api/some/somemethod/?id=1, neither of which are proper routing in Web API
    – David L
    Commented Nov 15, 2013 at 16:35
  • I do know that all, please see my updated question. It was problably unclear. Commented Dec 2, 2013 at 10:44
1

You can use the [FromBody] attribute on the parameter to force it to read from the body of the HTTP POST instead of the Uri. This is opposed to the [FromUri] attribute which does the opposite.

[HttpPost]
public void SomeAction([FromBody] int id)
{
    //do something with id    
}

Are you sure you're actually putting the id in the body? It could also be a routing issue. If this still doesn't work then maybe you should use Fiddler and copy the RAW output of your HTTP message here.

If you're packing multiple values into the body such as with JSON then you should use a model which should automatically be deserialized to:

public class PostModel
{
    public int ID { get; set; }
    public int SomeOtherID { get; set; }
}

[HttpPost]
public void SomeAction(PostModel postModel)
{
    //do something with postModel.ID and postModel.SomeOtherID
}
4
  • 1
    That might work in this simple example, but when I have 2 parameter I can only apply that tag once, applying it twice causes the error: Can't bind multiple parameters ('id' and 'id2') to the request's content. Commented Nov 15, 2013 at 16:23
  • You can't apply it twice because you don't have two HTTP bodies. You only have one body. If you are packing multiple values into the body then 1. How are you doing this? JSON? The format is important. And 2. You would need to create a model object to which to deserialize this complex body. I'll update my answer with an example. Commented Nov 15, 2013 at 16:26
  • I think the real question is how are you sending the request and why don't you just use query string variables? It's very easy to do. Post the code you are using to submit the POST message if you are not sure how to modify it. Commented Nov 15, 2013 at 16:31
  • Thanks for adding the deserialization example. If I hadn't stumbled across this I'd still be pounding my head against the keyboard. Guess I'm the only one that thinks this design is brain-damaged. (sigh).
    – jerhewet
    Commented Jul 24, 2014 at 15:29
0

You can actually do this straight out of the box in WebAPI, at least in 2.2 (.Net version 4.5.2). Your controller is correct. Using your controller, if you call it with a HTTP POST like this (tested through Fiddler):

http://localhost:58397/api/Reservation?roomId=123&arrivalDate=2015-12-17

You'll get the correct values of roomId = 123 and arrivalDate = 17.12.2015.

I suspect there's something wrong in your call to the WebAPI. Maybe post that call if you're still not getting it to work.

3
  • My whole point was that it shouldn't matter where the parameters are coming from (querystring or body), you are still adding the parameters to the querystring. In mvc I could place them in the body as well, but in the api project I can't. But as I concluded, mvc and the api libs are not the same (unfortunately) Commented Dec 18, 2015 at 14:00
  • @BlackHawkDesign: Are you saying you'd like the controller to pick up the parameters from either, so that you could call the method first with query string parameters only and then separately with body parameters only, and end up in the same method and get the same result? Commented Dec 18, 2015 at 14:09
  • I do say that it should pick from either (that's how mvc works), but not for the reason you mention. But the type of the parameter shouldn't determine the source. (And my question back than was why it worked that why). Commented Dec 18, 2015 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.