Why are the FromBody
and FromUri
attributes needed in ASP.NET Web API`?
What are the differences between using the attributes and not using them?
public HttpResponseMessage Post([FromBody] int id, [FromBody] string name) { ... } The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once.
The [FromUri] attribute is prefixed to the parameter to specify that the value should be read from the URI of the request, and the [FromBody] attribute is used to specify that the value should be read from the body of the request.
[FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.
The [FromBody] attribute can be applied on only one primitive parameter of an action method. It cannot be applied to multiple primitive parameters of the same action method.
The default behavior is:
If the parameter is a primitive type (int
, bool
, double
, ...), Web API tries to get the value from the URI of the HTTP request.
For complex types (your own object, for example: Person
), Web API tries to read the value from the body of the HTTP request.
So, if you have:
...then you don't have to add any attributes (neither [FromBody]
nor [FromUri]
).
But, if you have a primitive type in the body, then you have to add [FromBody]
in front of your primitive type parameter in your WebAPI controller method. (Because, by default, WebAPI is looking for primitive types in the URI of the HTTP request.)
Or, if you have a complex type in your URI, then you must add [FromUri]
. (Because, by default, WebAPI is looking for complex types in the body of the HTTP request by default.)
Primitive types:
public class UsersController : ApiController { // api/users public HttpResponseMessage Post([FromBody]int id) { } // api/users/id public HttpResponseMessage Post(int id) { } }
Complex types:
public class UsersController : ApiController { // api/users public HttpResponseMessage Post(User user) { } // api/users/user public HttpResponseMessage Post([FromUri]User user) { } }
This works as long as you send only one parameter in your HTTP request. When sending multiple, you need to create a custom model which has all your parameters like this:
public class MyModel { public string MyProperty { get; set; } public string MyProperty2 { get; set; } } [Route("search")] [HttpPost] public async Task<dynamic> Search([FromBody] MyModel model) { // model.MyProperty; // model.MyProperty2; }
From Microsoft's documentation for parameter binding in ASP.NET Web API:
When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object). At most one parameter is allowed to read from the message body.
This should work:
public HttpResponseMessage Post([FromBody] string name) { ... }
This will not work:
// Caution: This won't work! public HttpResponseMessage Post([FromBody] int id, [FromBody] string name) { ... }
The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once.
When the ASP.NET Web API calls a method on a controller, it must set values for the parameters, a process called parameter binding.
By default, Web API uses the following rules to bind parameters:
If the parameter is a "simple" type, Web API tries to get the value from the URI. Simple types include the .NET primitive types (int, bool, double, and so forth), plus TimeSpan, DateTime, Guid, decimal, and string, plus any type with a type converter that can convert from a string.
For complex types, Web API tries to read the value from the message body, using a media-type formatter.
So, if you want to override the above default behaviour and force Web API to read a complex type from the URI, add the [FromUri]
attribute to the parameter. To force Web API to read a simple type from the request body, add the [FromBody]
attribute to the parameter.
So, to answer your question, the need of the [FromBody]
and [FromUri]
attributes in Web API is simply to override, if necessary, the default behaviour as described above. Note that you can use both attributes for a controller method, but only for different parameters, as demonstrated here.
There is a lot more information on the web if you google "web api parameter binding".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With