Asp.net web api From Ri and From Body when to use?

Asked

Viewed 4,221 times

2

Asp.net web api that serves [Frombody] and [Fromuri] and when there is real need to use ?

example:

  public IHttpActionResult Get([FromUri] email)
  {

  }

  public IHttpActionResult Get([FromBody] email)
  {

  }

1 answer

2


There are two attributes for you to make explicit which will be the parameter Binding that your API will use. By default, the framework will map almost all objects as a JSON that comes in the request body (Datetime, Guid, are examples of objects that it will try to pick up from the URI), and all simple/primitive types as a value that will come in the request URI. So, if you want to do the reverse, you need to specify where the request is coming from.

Example, if you want to receive a string that comes in the body of your request, you must specify for the API: public IHttpActionResult GetPessoaPorNome([FromBody] string pessoa) {}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.