Introduction
Model binding in ASP.NET Core simplifies the process of transferring data between client-side requests and server-side code by automating the conversion of HTTP request data into .NET objects. This feature is particularly useful for handling various data sources, including query strings, form data, route parameters, JSON payloads, and custom binders.
Detailed guide to different Model binding attributes
1.[FromBody]
Binds request body data. For example, creating a user from a JSON request body.
[HttpPost("AddBanner")]
public async Task<IActionResult> CreateBanner(bannerRequestDTO bannerrequest)
{
try
{
var data = await this._bannerRepository.createbanner(bannerrequest);
return new CreatedResult(string.Empty, new
{
Code = 200,
Status = true,
Message = "DATA_INSERT_SUCCESS",
Data = new { }
});
}
catch (Exception ex)
{
return new CreatedResult(string.Empty, new
{
Code = 401,
Status = false,
Message = ex,
Data = new { }
});
}
}
2.[FromForm]
Binds form data. For example, uploading files through form data.
[HttpPost("Upload/location")]
public async Task<IActionResult> upload([FromForm] IFormFile file, string location)
{
try
{
var data = file upload logic business layer functionalities;
return new CreatedResult(string.Empty, new { Code = 200, Status = true, StatusCode = "", Data = data });
}
catch (Exception ex)
{
return new CreatedResult(string.Empty, new { Code = 401, Status = false, Message = ex, Data = new { } });
}
}
3.[FromHeader]
Binds data from HTTP request headers. For example, retrieving a custom header.
[HttpGet]
public IActionResult GetData([FromHeader] string customHeader)
{
}
4.[FromQuery]
Binds query string parameters. For example, implementing pagination.
[Authorize(Roles = "admin")]
[HttpGet("GetAllMenu")]
public async Task<IActionResult> GetAllMenu([FromQuery] int page, [FromQuery] int size)
{
try
{
return new CreatedResult(string.Empty, new { Code = 200, Status = true, Message = "", Data = data });
}
catch (Exception ex)
{
return new CreatedResult(string.Empty, new { Code = 401, Status = false, Message = ex, Data = new { } });
}
}
5.[FromRoute]
Binds route data. For example, accessing a specific resource by ID.
[HttpGet("GetMenuById/{Id}")]
public async Task<IActionResult> GetAllMenu([FromRoute] long Id)
{
try
{
return new CreatedResult(string.Empty, new { Code = 200, Status = true, Message = "", Data = data });
}
catch (Exception ex)
{
return new CreatedResult(string.Empty, new { Code = 401, Status = false, Message = ex, Data = new { } });
}
}
6.[FromServices]
Injects services into action methods. For example, using an IMenuRepository service.
[HttpGet("GetMenu")]
public async Task<IActionResult> GetAllMenu([FromServices] IMenuRepository repository)
{
try
{
var data = await repository.GetAllMenus();
return new CreatedResult(string.Empty, new { Code = 200, Status = true, Message = "", Data = data });
}
catch (Exception ex)
{
return new CreatedResult(string.Empty, new { Code = 401, Status = false, Message = ex, Data = new { } });
}
}
7.[AsParameters]
Combines multiple bindings (ASP.NET Core 7.0+). For example, aggregating parameters into a single class.
public class GetAllMenuRequestDTO
{
[FromQuery] public int Page { get; set; }
[FromHeader] public string CustomHeader { get; set; }
}
[HttpGet("GetMenu")]
public async Task<IActionResult> GetAllMenu([AsParameters] GetAllMenuRequestDTO request)
{
try
{
// Your logic here
return new CreatedResult(string.Empty, new { Code = 200, Status = true, Message = "", Data = data });
}
catch (Exception ex)
{
return new CreatedResult(string.Empty, new { Code = 401, Status = false, Message = ex, Data = new { } });
}
}
Conclusion
Understanding and using the right model binding attributes in ASP.NET Core Web API is crucial for efficient API development. This guide provides practical examples to help developers implement model binding effectively.
Please consider liking and following me for more articles and if you find this content helpful.