Introduction
Parameter binding in ASP.NET Web API is the process of mapping incoming request data to the parameters of a Web API action method. This allows the Web API framework to automatically extract values from various sources and bind them to the corresponding parameters, simplifying the handling of incoming data. In this documentation, we will explore the different types of parameter binding in ASP.NET Web API and provide code examples to illustrate their usage.
Table of Contents
- Query String Parameter Binding
- Example 1: Binding from the Query String
- Example 2: Binding from the Query String with Default Value
- Route Parameter Binding
- Example 1: Binding from the Route
- Example 2: Binding from the Route with Constraints
- Request Body Parameter Binding
- Example 1: Binding from the Request Body
- Example 2: Binding from the Request Body with Validation
1. Query String Parameter Binding
Query string parameter binding allows you to bind values from the query string portion of the request URL to the parameters of an action method.
1.1 Example 1: Binding from the Query String
csharp[HttpGet]
public IHttpActionResult GetProducts(string category)
{
// Use the 'category' parameter for further processing
// ...
}
In the above code snippet, the GetProducts
action method expects a category
parameter. When a request is made to this action method, the Web API framework automatically maps the value from the query string parameter named "category" to the category
parameter of the method.
1.2 Example 2: Binding from the Query String with Default Value
csharp[HttpGet]
public IHttpActionResult GetProducts(int page = 1, int pageSize = 10)
{
// Use the 'page' and 'pageSize' parameters for further processing
// ...
}
In this example, the GetProducts
action method accepts two optional parameters: page
and pageSize
. If these parameters are not provided in the query string, they will default to 1 and 10, respectively. However, if the values are present in the query string, they will be bound accordingly.
2. Route Parameter Binding
Route parameter binding allows you to bind values from the route template to the parameters of an action method.
2.1 Example 1: Binding from the Route
csharp[HttpGet]
[Route("api/products/{id}")]
public IHttpActionResult GetProductById(int id)
{
// Use the 'id' parameter for further processing
// ...
}
In the above code snippet, the GetProductById
action method expects an id
parameter. When a request is made to this action method with a matching route, such as "/api/products/123", the value "123" will be automatically bound to the id
parameter.
2.2 Example 2: Binding from the Route with Constraints
csharp[HttpGet]
[Route("api/products/{id:int}")]
public IHttpActionResult GetProductById(int id)
{
// Use the 'id' parameter for further processing
// ...
}
In this example, the GetProductById
action method expects an id
parameter of type int
. The route constraint :int
ensures that only integer values are matched by this route. If a non-integer value is provided in the route, the request will not match this action method.
3. Request Body Parameter Binding
Request body parameter binding allows you to bind complex data from the request body to the parameters of an action method.
3.1 Example 1: Binding from the Request Body
csharp[HttpPost]
public IHttpActionResult CreateProduct(ProductDto product)
{
// Use the 'product' parameter for further processing
// ...
}
In the above code snippet, the CreateProduct
action method expects a parameter of type ProductDto
, which represents the data structure of the product to be created. The Web API framework automatically deserializes the request body into an instance of ProductDto
and binds it to the product
parameter.
3.2 Example 2: Binding from the Request Body with Validation
csharp[HttpPost]
public IHttpActionResult CreateProduct([FromBody] ProductDto product)
{
if (!ModelState.IsValid)
{
// Handle invalid model state
return BadRequest(ModelState);
}
// Use the 'product' parameter for further processing
// ...
}
In this example, the CreateProduct
action method also expects a parameter of type ProductDto
. The [FromBody]
attribute explicitly indicates that the data should be read from the request body. Additionally, model validation is performed using ModelState.IsValid
to ensure the received data is valid according to the specified validation rules in the ProductDto
class.
These examples demonstrate the different ways to perform parameter binding in ASP.NET Web API. Understanding these techniques will help you efficiently handle incoming data and build robust Web API applications.
0 Comments