The ASP.NET Web API is a framework for building web APIs on top of the .NET framework. It uses HTTP as an application protocol (rather than a transport protocol) to return data based on the client requests. The Web API is able to return the data appropriately based on the media type specified with the request. By default it provides JSON based responses. CRUD stands for "Create, Read, Update, and Delete," which are the four basic database operations. Many HTTP services also model CRUD operations through REST or REST-like APIs.
The four main HTTP methods (GET, PUT, POST, PATCH and DELETE) can be mapped to CRUD operations as follows:
- GET retrieves the representation of the resource at a specified URI. GET should have no side effects on the server.
- PUT updates a resource at a specified URI. PUT can also be used to create a new resource at a specified URI, if the server allows clients to specify new URIs. For this tutorial, the API will not support creation through PUT.
- PATCH updates data in database (not all fields are required).
- POST creates a new resource. The server assigns the URI for the new object and returns this URI as part of the response message.
- DELETE deletes a resource at a specified URI.
Enabling OData Query Options
Web API supports the following OData query options:
- $expand - Expands related entities inline
- $filter - Filters the results, based on a Boolean condition
- $orderby - Sorts the results
- $select - Selects which properties to include in the response
- $skip - Skips the first n results
- $top - Returns only the first n the results
Using $select, $expand, and $value in Web API
Web API 2 adds support for the $expand, $select, and $value options in OData. These options allow a client to control the representation that it gets back from the server.
$expand causes related entities to be included inline in the response.
$select selects a subset of properties to include in the response.
$value gets the raw value of a property.