
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is parameter binding in C# ASP.NET WebAPI?
Binding is a process to set values for the parameters when Web API calls a controller action method.
Web API methods with the different types of the parameters and how to customize the binding process.
If the parameter is a simple type like int, bool, double, etc., Web API tries to get the value from the URI (Either from route data or from the Query String)
if the parameter is a complex type like Customer, Employee, etc., then the Web API Framework tries to get the value from the request body.
We can change this default behavior of the parameter binding process by using [FromBody] and [FromUri] attributes.
FromUri −
If the parameter is of simple type, then web Api tries to get the value from the URI
.NET Primitive type like double,DateTime,GUID string any type which can e converted from the String type
Example
public Student Get(int id){}
FromBody
If the parameter of type Complex type, then Web Api will try to bind the values from the message body.
Example
Public Student Post(Employee employee){}
[FromUri]
To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter
Use [FromUri] attribute to force Web Api to get the value of Complex type from QueryString.
Example
public Student Get([FromUri] Employee employee) public HttpResponseMessage Get([FromUri] Employee employee) { ... }
[FromBody]
Use [FromBody] attribute to get the value of Primitive type from the request body, opposite to the default values
No, multiple FormBody is not allowed in a single action.
To force Web API to read a simple type from the request body, add the [FromBody]
In this example, Web API will use a media-type formatter to read the value of name from the request body
Example
public Student Post([FromBody] string name]){...} public HttpResponseMessage Post([FromBody] string name) { ... }
- Related Articles
- How to do versioning with the Querystring parameter in C# ASP.NET WebAPI?
- What is Content Negotiation in Asp.Net webAPI C#?
- What is the usage of DelegatingHandler in Asp.Net webAPI C#?
- What is the use of Authorize Attribute in C# Asp.Net webAPI?
- What are built-in message handlers in Asp.Net webAPI C#?
- What are the advantages of using C# ASP.NET WebAPI?
- What are the different types of filters in C# ASP.NET WebAPI?
- How can we test C# Asp.Net WebAPI?
- How to resolve CORS issue in C# ASP.NET WebAPI?
- How to configure C# ASP.NET WebAPI in web.configure file?
- How do we specify MIME type in Asp.Net WebAPI C#?
- What are the various return types of a controller action in C# ASP.NET WebAPI?
- How to do versioning with accept header in C# ASP.NET WebAPI?
- How to do Web API versioning with URI in C# ASP.NET WebAPI?
- How to do versioning with custom media type in C# ASP.NET WebAPI?
