locked
What is [formbody] usage in web api RRS feed

  • Question

  • User264732274 posted

    i need to know what [formbody] denotes and what it is ?

    when to use [formbody] ?

    we normally send data from client side to server side and data is automatically deserialize to custom collection

    in asp.net mvc suppose we have registration form and we pass form's data in json format from client side to server side function and server side function look like

    [HttpPost]
    public ActionResult Example2(Employee emp)
    {
         ViewBag.StatusMessage = "Employee data received successfully for ID " + emp.Id + "!";
         return View();
    }

    so when we pass data from client side to server side function then why we need [formbody] ?

    what is the significant of [formbody] usage in server side action ?

    also tell me can we use [formbody] in asp.net mvc action or is it only specific for web api ?

    thanks

    Wednesday, July 13, 2016 11:14 AM

Answers

  • User475983607 posted

    IMHO, Parameter Binding in ASP found by clicking the Learn link in the menu above does a great job explaining how [FormBody] as well as other binders work.  The doc also has links to other resources.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 13, 2016 1:58 PM
  • User36583972 posted

    Hi sudip_inn,

    As far as I know, In Web API/MVC, you can use [FromUri] [FromBody] attributes to set values for the parameters. By default, the 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 have a primitive type in the URI or if you have a complex type in the body, then you don't have to add any attributes (not [FromBody] nor [FromUri]).

    If you have a primitive type in the body, then you have to add [FromBody] in front of your primitive type in your WebAPI controller (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).

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 14, 2016 2:27 AM

All replies

  • User475983607 posted

    IMHO, Parameter Binding in ASP found by clicking the Learn link in the menu above does a great job explaining how [FormBody] as well as other binders work.  The doc also has links to other resources.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 13, 2016 1:58 PM
  • User36583972 posted

    Hi sudip_inn,

    As far as I know, In Web API/MVC, you can use [FromUri] [FromBody] attributes to set values for the parameters. By default, the 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 have a primitive type in the URI or if you have a complex type in the body, then you don't have to add any attributes (not [FromBody] nor [FromUri]).

    If you have a primitive type in the body, then you have to add [FromBody] in front of your primitive type in your WebAPI controller (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).

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 14, 2016 2:27 AM
  • User1869050296 posted

    As mentioned by Yohann frombody is used when you need to pass objects instead of simple int/string params to your controller

    Monday, July 18, 2016 1:51 AM