Web API
provides a simple yet powerful framework for building
REST-based
services that can be consume by
a broad range of clients including browsers, mobiles, iphone and tablets.
With WebAPI content negotiation, we can return data based on the client
requests. What I mean is, if the client is requesting the data to be returned
as JSON or XML, the WebAPI framework deals with the request type and returns
the data appropriately based on the media type. By default WebAPI provides JSON
and XML based responses.
Let’s look at just a few of these new features:
- Convention-based CRUD Actions:
HTTP actions (e.g., GET and POST) are automatically mapped to controller methods(also known as controller actions) by their names. For example,on a controller called Products, a GET request such as/api/products will automatically invoke a method named “Get”on the controller. Further, the Web API automatically matches the number of arguments given in the URL to an appropriate controller method. Therefore, the URL /api/products/32 would automatically invoke the Get(long id) method. The same magic also applies to POST, PUT, and DELETE calls. - Built-in Content Negotiation: In
MVC, controller methods that return JSON or XML have to be hard-coded to
specifically return one of those content types. But with the Web
API, the controller method need only return the raw data value, and
this value will be automatically converted to JSON or XML, per the
caller’s request.The caller simply uses an Accept or
Content-Type HTTP header to specify the desired content type of the
returned data, and theWeb API ensures your return value gets
formatted appropriately.Rather than returning an object of type JsonResult, you simplyreturn your data object (e.g., Product or IEnumerable<Product>).
- Automatic support for OData: By simply placing the new[Queryable] attribute on a controller method that returns IQueryable, clients can use the method for OData query
composition.
- Self-hosting: With the Web API, you no longer need to use IIS to host HTTP services. Now your REST services can be hosted in a custom Windows service, console application, or any other type of host you need.
No comments:
Post a Comment