ASP.NET Web API-Parameter Binding


In this article, we learn how to map Web API methods with the different types of the parameters and how to customize the binding process. In ASP.NET Web API, the Primitve type, complex type, [FromBody] and [FromUri] attributes are used to specify how parameters should be bound from the request data.

The following figure summarizes parameter binding rules.

ASP.NET web API Parameter Binding

1. Primitive Type Binding

Consider the following example of the GET action method that includes a single primitive type parameter.

Example

public class BooksController : ApiController
{
    public Book Get(int id) 
    {
              
    }
}

The followings are valid HTTP GET Requests for the above action method.

http://localhost/api/Book?id=1

Multiple Primitive Parameters

Consider the following example of the GET action method with multiple primitive parameters.

Example

public class BooksController : ApiController
{
    public Book Get(int id, string name) 
    {
              
    }
}

The followings are valid HTTP GET Requests for the above action method.

http://localhost/api/Book?id=1&name=science

2. Complex Types Binding

We can change the default manner of Model Binding for Complex Types by using [Bind(Prefix)] Attribute . This is a case when we want to bind to a different object than what the Model Binding feature is looking.

Let us understand this with an example. Create a new Model class called EmployeeAddress.cs with the following code.

namespace ModelBindingValidation.Models
{
    public class EmployeeAddress
    {
        public string City { get; set; }
        public string Country { get; set; }
    }
}

Next, add a new action called DisplayPerson to the HomeController class.

[HttpPost]
public IActionResult DisplayEmployee(EmployeeAddress employeeAddress)
{
    return View(employeeAddress);
}

3. [FromBody] Primitive Type Binding

The [FromBody] attribute is used to bind complex types or multiple parameters from the request body. It is typically used when sending data in JSON or XML format. When a parameter is decorated with [FromBody], Web API uses the configured media-type formatter to deserialize the request body into the specified parameter type.

Example

[HttpPost]
public IHttpActionResult CreateBook([FromBody] bookDto bookDto)
{
     // Process the bookDto received from the request body
    return Ok();
}

In the above example, the CreateBook action method expects a complex type BookDto to be sent in the request body as JSON or XML. The [FromBody] attribute instructs Web API to bind the request body to the bookDto parameter.

4. [FromUri] Complex Type Binding

The [FromUri] attribute is used to bind simple types or single parameters from the URI or query string. By default, simple types are bound from the URI or query string, so the [FromUri] attribute is not required in most cases.

Example

[HttpGet]
public IHttpActionResult Getbook([FromUri] int id)
{
    // Retrieve the book with the specified id from the database
    return Ok(book);
}

In the above example, the Getbook action method expects a simple type int to be sent as part of the URI or query string. The [FromUri] attribute is used to explicitly indicate that the id parameter should be bound from the URI or query string.


Next

Top Articles

  1. Why use C#
  2. How to use comment in C#
  3. How to use variables in C#
  4. How to use keywords in C#