WebAPI is a framework used for developing HTTP services.
Like MVC application WebAPI also uses controller and action methods to handle the requests.Similar to MVC WebAPI also supports concepts such as:
- Controllers and action methods
- Routing
- Filters
- Model Binding
But it differs from MVC
Unlike MVC application WebAPI application can not return views
Unlike MVC WebAPI controllers are derived from APIController instead of Controller base class.
Whenever a request is received it is mapped to the action method in controller in following manner
1.If the action method has specified any HTTP method attribute such as [HTTGET] ,[HTTPPOST] then the action method handles that HTTP verb.For example the following action method will handle the get request.
2.If the action method name starts with the name of any verb then that action method will handle that specific request.The below action method will handle the request for GET.
public IEnumerable<Employee> GetAll() { return EmployeeList; }
Following will also handle the Get request
public Employee Get(int id) { Employee e = EmployeeList.Where(x => x.Id == id).FirstOrDefault(); return e; }
3.If none of the above are specified then the action method handles POST request by default.
Leave a Reply