What the difference between [FromRoute]
and [FromBody]
in a Web API?
[Route("api/Settings")]
public class BandwidthController : Controller
{
// GET: api/Settings
[HttpGet]
public IEnumerable<Setting> GetSettings()
{
return _settingRespository.GetAllSettings();
}
// GET: api/Settings/1
[HttpGet("{facilityId}", Name = "GetTotalBandwidth")]
public IActionResult GetTotalBandwidth([FromRoute] int facilityId)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
}
}
Also for PUT
:
// PUT: api/Setting/163/10
[HttpPut]
public void UpdateBandwidthChangeHangup([FromRoute] int facilityId, int bandwidthChange)
{
_settingRespository.UpdateBandwidthHangup(facilityId, bandwidthChange);
}
Can I use [FromBody]
?
When you use
FromBody
attribute you are specifying that the data is coming from the body of the request body and not from the request URL/URI. You cannot use this attribute withHttpGet
requests, only with PUT,POST,and Delete requests. Also you can only use oneFromBody
attribute tag per action method in Web API (if this has changed in mvc core I could not find anything to support that).Essentially it
FromRoute
will look at your route parameters and extract / bind the data based on that. As the route, when called externally, is usually based on the URL. In previous version(s) of web api this is comparable toFromUri
.So this would try to bind
facilityId
based on the route parameter with the same name.Edit
Based on your last question, here is the corresponding code assuming you want 163 to be bound to facilityId and 10 to bandwidthChange parameters.
If you had a complex object in one of the parameters and you wanted to send this as the body of the Http Request then you could use
FromBody
instead ofFromRoute
on that parameter. Here is an example taken from the Building Your First Web API with ASP.NET Core MVCThere are also other options in MVC Core like
FromHeader
andFromForm
andFromQuery
.