ilhan DEMİRTEPE
ParamTech
Published in
2 min readJan 6, 2023

--

Model Binding NET Core Web API Actions

Web APIs receives the requests in the form of HTTP messages. Every http message has headers, URLs, query strings parameters, request body, etc.

Why use model binding

  • Retrieving data from various sources such as route data, form fields, and query strings.
  • Providing this data to input parameters of Actions. For Razor pages, there can be class level properties binding to the incoming request data.
  • Converting the string data from incoming request to appropriate target .NET data type
  • Populating complex data types used as input parameters

From Route

The value for input parameters for an Action might come from route parameters.

From Query

The input parameters can also take values from query string.

From Body

This attribute is required to be placed explicitly and it cannot be inferred.

Generally this attribute is used with Complex types (i.e. class). If a class has any other model binding attributes specified on individual properties, then it is ignored by input formatter.This attribute is required to be placed explicitly and it cannot be inferred.

When this attribute is placed on an input parameter, the value of that parameter is populated from HTTP request body. Responsibility of handling populating the parameter is delegated to input formatters.

From Headers

Sometimes some important data is passed via headers.

1-)Test with swagger

2-)Test with Postman

--

--