Parameter Binding in ASP.Net Web API

Introduction

This article explains parameter binding in the ASP.NET Web API. Parameter binding is a type for catching values from the URI and from the message body by the Web API. These depend on the type of the parameter.

There are various rules for binding the parameters:

  • "Simple" type: If the parameter is the simple type then it is a string convertible parameter that includes the preemptive data types such as Int, Double, Bool and so on with the Date Time, Decimal, string and so on. By default these are read from the URI.
  • "Complex" type: If the parameter is a complex type then the Web API catches the value from the message body. It uses the media type formatters for catching the value from the body.

  1. HttpResponseMessage Get(int No, Item value)  
  2. {  
  3.     ....  
  4. }

The preceding is an example in which the parameter "No" is a simple type so the value of this parameter is caught from the URI. and the second parameter "value" that is a complex type so the value of this parameter is caught from the body.

From URI binding

In the From URI binding we use the [FromUri] attribute. The [FromUri] attribute is inherited from the [ModelBinder] attribute. Let's see an example:

 

  1. public class Product  
  2. {  
  3.     public int pageNo { getset; }  
  4.     public decimal Price { getset; }  
  5.     public int size { getset; }  
  6. }  

 

We use the [FromUri] attribute that is bound in this example from the URI.

  1. public class ValuesController : ApiController  
  2. {  
  3.     HttpResponseMessage Get([FromUri] Product product)  
  4.     {  
  5.     .......  
  6.     }  
  7. } 

The user puts the value of the pageNo, Price and size from the query string and the Web API reads these values from the URI and uses it for the "Product".

GET/products?pageNo=1&price=20&size=xxl

From Body binding

In the Body binding we use the [FromBody] attribute. That allows the Web API to catch the value of the parameters from the body.

  1. public HttpResponseMessage Post([FromBody] string Address)  

Model Binding

The model binding is used for both the URI and the message body. We can say that in the model binding the framework determines the properties of the model in both the URI and body and then it attempts to determine the result together after running it. It can be done because the body of the request is being loaded by the MVC that can determine the key value pairs and then produce the model.

When we create the model binder then we implement the class with the "IModelBinder" interface. This interface includes a method, "Bind Model".

bool BindModel(HttpActionContext context, ModelBindingContext contextb);

Let's see a sample of code using ModelBinder:

  1. public class ProductBinder : IModelBinder  
  2. {  
  3.     private static ConcurrentDictionary<string, Product> products  
  4.      = new ConcurrentDictionary<string, Product>(StringComparer.OrdinalIgnoreCase);  
  5.     static ProductBinder()  
  6.     {  
  7.         products["redmond"] = new Product() { pageNo = 2, Price = 10, size = 20 };  
  8.         products["paris"] = new Product() { pageNo = 3, Price = 30, size = 40 };  
  9.         public bool BindModel(HttpActionContext context, ModelBindingContext contextb)  
  10.         {  
  11.             .....  
  12.             .....  
  13.         }  
  14.     }  
  15. } 

We use the namespace "using System.Web.Http.ModelBinding" that has the reference of the "BindModel" method and "IModelBinder" interface. The "BindModel" method binds the model to a value using the specified controller context and binding context.

"ModelBindingContext" initializes the new instance of the ModelBindingContext class.

"HttpActionContext" initializes the instance of the HttpActionContext class.

 It is possible to bind the entire model with the URI used in the GET request. In it you can provide the complex conditions. And you can also bind the Model with the message body.


Similar Articles