Last post Jul 18, 2016 01:51 AM by jmpalines
Jul 13, 2016 11:14 AM|sudip_inn|LINK
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
public ActionResult Example2(Employee emp)
ViewBag.StatusMessage = "Employee data received successfully for ID " + emp.Id + "!";
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 ?
Jul 13, 2016 01:58 PM|mgebhard|LINK
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.
Jul 14, 2016 02:27 AM|Yohann Lu|LINK
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).
Jul 18, 2016 01:51 AM|jmpalines|LINK
As mentioned by Yohann frombody is used when you need to pass objects instead of simple int/string params to your controller