Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 RC Web API Parameter Binding Issue

I'm getting what I consider to be a weird binding issue in the ASP.NET MVC 4 RC Web API. I have a method intended to accept post requests from the client. The issue is that none of the parameters are binding when the post method is called, I get to my break point on the throw line and name, email are both null. If I change the type of the request to GET in the JavaScript, then the Get function below is called with parameters bound.

Why are the parameters failing to bind for the Post method, and how can I fix this?

send: function(evt) {
    evt.preventDefault();
    $.ajax( {
        url: '/api/person',
        data: this.model.toJSON(),
        type: "POST",
        dataType: "json",
        success: function(data) {
            console.log("Success");
        },
        error: function(data) {
            console.log("Error");
        }
    });
     }

The following is the controller actions:

public void Get(string name, string email) {
    throw new NotImplementedException();
}

public void Post(string name, string email) {
    throw new NotImplementedException();
}

Notes:

  • I'm using all of the defaults for ASP.NET MVC 4 RC Web API (So the deserializer should be Json.NET)
  • The Chrome network tab on the JS debugger shows the parameters in the form data on post correctly.
like image 897
Cody Avatar asked Jun 22 '12 14:06

Cody


People also ask

How do we do parameter binding in Web API?

When Web API calls a method on a controller, it must set values for the parameters, a process called binding. By default, Web API uses the following rules to bind parameters: If the parameter is a "simple" type, Web API tries to get the value from the URI.

What is difference between FromQuery and FromBody?

[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data. [FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.

Does MVC use data binding?

MVC doesn't use data bindings like old web api. You have to use model bindings in a MVC or MVVM approach.


1 Answers

Unlike MVC (web pages), simple parameters types will not, by default, bind from the post body but instead from the URI. So, with your code as-is you would need to pass the name and email parameters in the query string or as route parameters.

However this can easily be solved by creating a model type (in MVC vernacular) and using that for the method parameters. In fact you can then use it for both (in the case that you've given), if you then use [FromUri] on the get method:

public class SomeParams {
  public string name { get; set; }
  public string email { get; set; }
}

//now an alternative way to write the Get method
public MyResult Get([FromUri] SomeParams p){
  //members are bound from the query string (more like MVC traditional binding)
  //note - as in MVC, SomeParams will need a default constructor for this to work.
}

public PostResult Post(SomeParams p){
  //'p' is bound from your JSON (assuming correct format)
  //because 'complex' types are deserialized using formatters
  //only one object can be read from the body with a formatter in Web API
  //as the request body is not buffered; unlike MVC.
}

I've stuck in return types for the methods just because they would need to return something!

I really do recommend reading through Mike Stall's article that I link to above (and many of his others).

It's tempting to assume that the Web API, because it shares the same paradigms and even class names of MVC, is actually the same as MVC - but it's not. I wondered initially why this was the case (as I've written a lot of REST services on top of MVC myself and found it to be pretty darn cool, once you've written a few utility classes and base-class enhancements), but they've taken a proper look at the challenges of writing a web API and I think are probably right to have made the changes in approach that they have.

However, it does mean that we have to take a few things we might now take for granted and re-learn them for the Web API.

like image 53
Andras Zoltan Avatar answered Oct 01 '22 00:10

Andras Zoltan