Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between FromBody and FromUri attributes in C# ASP.NETnWebAPI?
In ASP.NET Web API, parameter binding determines how action method parameters receive values from HTTP requests. The [FromUri] and [FromBody] attributes control the source of parameter data, providing explicit control over this binding process.
The FromUri attribute forces Web API to bind parameters from the URI query string, route data, or headers instead of the request body. The FromBody attribute instructs Web API to read parameter values from the HTTP request body using formatters like JSON or XML.
Syntax
Following is the syntax for using [FromUri] attribute −
public IActionResult Method([FromUri] ModelClass model) {
// binds from query string or route
}
Following is the syntax for using [FromBody] attribute −
public IActionResult Method([FromBody] ModelClass model) {
// binds from request body
}
Key Differences
| Aspect | FromUri | FromBody |
|---|---|---|
| Data Source | URI query string, route data, headers | HTTP request body |
| Content Type | application/x-www-form-urlencoded | application/json, application/xml |
| Complex Objects | Flattened into query parameters | Maintains object structure |
| Usage | Simple types, small data | Complex objects, large data |
Using FromUri Attribute
The [FromUri] attribute binds parameters from the URI components. This is useful for simple data types and when you want to pass data via query strings −
Example
using System;
using System.Collections.Generic;
namespace WebAPIDemo
{
public class DemoController
{
public IEnumerable<string> Get([FromUri] string id, [FromUri] string name)
{
return new string[]
{
$"The Id of the Student is {id}",
$"The Name of the Student is {name}"
};
}
public static void Main()
{
// Simulate URI: /api/demo?id=1&name=Mark
var controller = new DemoController();
var result = controller.Get("1", "Mark");
foreach (string item in result)
{
Console.WriteLine(item);
}
}
}
}
The output of the above code is −
The Id of the Student is 1 The Name of the Student is Mark
Using FromBody Attribute
The [FromBody] attribute reads complex objects from the request body, typically formatted as JSON or XML −
Student Model
using System;
using System.Collections.Generic;
namespace WebAPIDemo
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
public class DemoController
{
public IEnumerable<string> ProcessStudent([FromBody] Student student)
{
return new string[]
{
$"The Id of the Student is {student.Id}",
$"The Name of the Student is {student.Name}"
};
}
public static void Main()
{
// Simulate request body: {"Id": 123, "Name": "Alice"}
var controller = new DemoController();
var student = new Student { Id = 123, Name = "Alice" };
var result = controller.ProcessStudent(student);
foreach (string item in result)
{
Console.WriteLine(item);
}
}
}
}
The output of the above code is −
The Id of the Student is 123 The Name of the Student is Alice
Common Use Cases
Use FromUri when:
Passing simple parameters like IDs, flags, or small strings
Creating RESTful URLs that are bookmarkable
Working with GET requests where data should be in the URL
Use FromBody when:
Sending complex objects with multiple properties
Working with POST/PUT requests containing JSON or XML data
Handling large amounts of data that would make URLs unwieldy
Conclusion
The [FromUri] attribute binds parameters from URL components like query strings and route data, while [FromBody] reads data from the HTTP request body. Choose [FromUri] for simple parameters and [FromBody] for complex objects to ensure proper parameter binding in your Web API controllers.
