Doing Parameter Binding with the ASP.NET Web API

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

The eMVC Web API is all about Action controllers and Action methods. GET, POST, PUT, DELETE, and so forth are the action methods, which might take input parameters. Parameter Binding is the technique of how the value from the request is bound to the method parameter during runtime.

In this article, let us learn about parameter binding and few basic ways with which it can happen in ASP.NET MVC Web API applications.

FromUri and FromBody

The two basic options in Web API parameter binding are decorating the action method parameters as FromUri or FromBody. Let us look at these two options in this section.

FromUri

The parameter value is read from the request URI. Following is a sample ActionMethod that reads the name and age from the Uri.

      public string Get([FromUri]string name, [FromUri]int age)
      {
         //TODO
         return "value";
      }

The request URL should be as follows: http://localhost:12558/api/FromUriSample?name=Gavin&age=23

FromBody

The parameter value is read from the body of the request. Here is a sample ActionMethod and the request to bind the parameter value from the request body.

      public void Put([FromBody]string name)
      {
         //TODO Put operation
      }

PUT http://localhost:12558/api/FromBodySample HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:12558
Content-Length: 10

name=Gavin

Rules and Default Behaviors

There are some rules and default behaviors for the parameter binding. They are as follows:

  • The API reads from the Uri by default if the parameter type is a simple one like string, integer, and so on.
  • The API reads from the body if the parameter is of a complex type, like a POCO object.
  • You can define a TypeConverter for a POCO object and make it read from the Uri by default.
  • When reading from the request body, the API expects only one parameter value.
  • To read the data from the body, the API uses the formatter based on the Content-Type that is passed in the request header.

Binding a Generic List Parameter

If the parameter is of an array, or even a generic list, it can also be bound as shown in the following code section.

      public void Post([ModelBinder]List<int> numbers)
      {
         var n = numbers;
      }

Note that, in the preceding code, you will have to use the ModelBinder instead of FromUri or FromBody. The request should be as follows:

POST http://localhost:12558/api/
   FromUriSample?numbers=1&numbers=2 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost:12558
Content-Length: 7

JSON Binding Example

The parameter also can be bound from a JSON object in the request at runtime. In this section, let us create some sample code to illustrate.

Create the following model class that defines the data member names too.

   [DataContract]
   public class Employee
   {
      [DataMember(Name="fn")]
      public string FirstName { get; set; }
      [DataMember(Name = "ln")]
      public string LastName { get; set; }
      [DataMember(Name = "age")]
      public int Age { get; set; }
   }

Here is the ActionMethod accepting the Employee parameter.

      public void Post([FromBody]Employee employee)
      {
         //TODO save employee
      }

The Web API post request with a JSON body:

POST http://localhost:12558/api/FromBodySample HTTP/1.1
Content-Type: application/json
Host: localhost:12558
Content-Length: 7

{
   fn: "David",
   ln: "Richard",
   age: 24
}

Custom Implementation Options

There are also many custom implementations that you can do. Following are a couple of them:

  • Custom ModelBinder
  • Custom ValueProvider
  • Custom HttpParameterBinding
  • Custom TypeConverter

I hope this article was informative and is a good kick start to the readers who are new to developing ASP.NET MVC Web API applications.

Happy reading!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read