Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does ASP.NET Web Api model binding uses the parameter type to determine the source of the value?
    text
    copied!<p>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:</p> <pre><code>public class ReservationController : ApiController { [HttpPost] public void Create(int roomId, DateTime arrivalDate) { //do something with both parameters } } </code></pre> <p>This code is not working when I fire a post request at it, I'm receiving a 404 exception something like this:</p> <blockquote> <p>No action was found on the controller 'Some' that matches the request.</p> </blockquote> <p>The reason for it is that simple types are read from the query string, complex types from the body, according to this <a href="http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api" rel="nofollow">aricle</a>. The web api uses the parameters to match the action to a request and can't therefore map my action to the request.</p> <p>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.</p> <p>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: </p> <p><strong>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?</strong></p> <p>Best regards,</p> <p>BHD </p> <p><strong>FINAL UPDATE</strong> 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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload