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.

HttpResponseMessage Get(int No, Item value)

        {

        ....

        }

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:

public class Product

    {

        public int pageNo { get; set; }

        public decimal Price { get; set; }

        public int size { get; set; }

    }

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

public class ValuesController : ApiController

    {

        HttpResponseMessage Get([FromUri] Product product)

        {

        .......

        }

    }

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.

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);

Lets see a sample of code using ModelBinder:

public class ProductBinder : IModelBinder

    {

        private static ConcurrentDictionary<string, Product> products

         = new ConcurrentDictionary<string, Product>(StringComparer.OrdinalIgnoreCase);

 

        static ProductBinder()

        {

            products["redmond"] = new Product() { pageNo = 2, Price = 10, size = 20 };

            products["paris"] = new Product() { pageNo = 3, Price = 30, size = 40 };

 

       

 

        public bool BindModel(HttpActionContext context, ModelBindingContext contextb)

        {

            .....

            .....

        }

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.

Up Next
    Ebook Download
    View all
    Learn
    View all