ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
Video :
https://youtu.be/qwLBeg1CPSo
Courtesy:
http://www.ifourtechnolab.com
2. ASP.NET Web API
Why ASP.NET MVC Web API?
ASP.NET Web API Features
ASP.NET Web API Design
ASP.NET MVC Web API
Routing Table
Error & Exception Handling
Model Validation
INDEX
http://www.ifourtechnolab.com/ C# Software Development Companies India
3. Framework for building and consuming HTTP services that can reach a broad range of
clients including browsers, phones and tablets
Use XML or JSON or something else with API. JSON is nice for mobile apps with slow
connections
For example: Call an API from jQuery and better utilize the client's machine and browser
What is ASP.NET Web API?
http://www.ifourtechnolab.com/ C# Software Development Companies India
4. It is built for all the other, non-human interactions website or service needs to support.
Think about jQuery code that's making an Ajax request, or a service interface that supports
a mobile client
In these cases, the requests are coming from code and expect some kind of structured
data and specific HTTP Status Codes
These two are very complimentary, but different enough that trying to build out HTTP
services using ASP.NET MVC took a lot of work to get right
Why ASP.NET MVC Web API?
http://www.ifourtechnolab.com/ C# Software Development Companies India
5. Modern HTTP programming model
Directly access and manipulate HTTP requests and responses in Web APIs using a new, strongly
typed HTTP object model
Full support for routes
Web APIs now support the full set of route capabilities that have always been a part of the Web
stack, including route parameters and constraints
Additionally, mapping to actions has full support for conventions, so no longer need to apply
attributes such as [HttpPost] to your classes and methods
Filters
Web APIs now supports filters, including well-known filters such as the [Authorize] attribute
ASP.NET Web API Features
http://www.ifourtechnolab.com/ C# Software Development Companies India
6. Content negotiation
The client and server can work together to determine the right format for data being returned
from an API
Provide default support for XML, JSON, and Form URL-encoded formats, and extend this support
by adding your own formatters
Model binding and validation: Model binders provide an easy way to extract data from various
parts of an HTTP request and convert those message parts into .NET objects which can be used
by the Web API actions
Improved testability of HTTP details
Rather than setting HTTP details in static context objects, Web API actions can now work with
instances of HttpRequestMessage and HttpResponseMessage
Generic versions of these objects also exist to work with custom types in addition to the HTTP
types
ASP.NET Web API Features (Cont.)
http://www.ifourtechnolab.com/ C# Software Development Companies India
7. Query composition
By simply returning IQueryable<t>, Web API will support querying via the OData URL
conventions
Improved Inversion of Control (IoC) via DependencyResolver
Web API now uses the service locator pattern implemented by MVC’s dependency resolver to
obtain instances for many different facilities
Code-based configuration
Web API configuration is accomplished solely through code, leaving config files clean
Self-host
Web APIs can be hosted in own process in addition to IIS while still using the full power of routes
and other features of Web API
ASP.NET Web API Features (Cont.)
http://www.ifourtechnolab.com/ C# Software Development Companies India
8. The client is whatever consumes the web API (browser, mobile app, and so forth)
A model is an object that represents the data in application. In this case, the only model is
a to-do item. Models are represented as simple C# classes (POCOs)
A controller is an object that handles HTTP requests and creates the HTTP response
ASP.NET Web API Design
http://www.ifourtechnolab.com/ C# Software Development Companies India
9. MVC : Model -> View -> Controller
Create a Model
Create a Controller
ASP.NET MVC Web API
http://www.ifourtechnolab.com/ C# Software Development Companies India
10. It is defined, by default, as api/{controller}/{id} where action is defined by an HTTP method
(in global.asax Application_Start method)
Routing Table
HTTP Method URI Path Action Performed
GET /api/products Get All Products
GET /api/products/id Get Product by Id
GET /api/products?category=category Get Products by Category
POST /api/products Insert Product
PUT /api/products/id Update Product
DELETE /api/products/id Delete Product
http://www.ifourtechnolab.com/ C# Software Development Companies India
11. The four main HTTP methods are mapped to CRUD operations:
Get retrieves the representation of the resource at a specified URI. GET should have no side
effects on server
PUT updates a resource at a specified URI
POST Create 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
Routing Table
http://www.ifourtechnolab.com/ C# Software Development Companies India
12. ASP.NET MVC Web API - Example
APIs for crud operation for products
http://www.ifourtechnolab.com/ C# Software Development Companies India
13. Response messages, errors and exceptions are translated to HTTP response status codes
For example:
POST request should reply with HTTP status 201 (created)
DELETE request should reply with HTTP status 204 (no content)
But:
If a PUT/GET request is done with an invalid id, it should reply with HTTP status 404 (not found)
Or if a PUT request has invalid model, it can reply with HTTP status 400 (bad request)
Error & Exception Handling
http://www.ifourtechnolab.com/ C# Software Development Companies India
14. By default, all .NET exceptions are translated into an HTTP response with status code 500
(internal error)
It is possible to register Exception filters:
Error & Exception Handling (Cont.)
http://www.ifourtechnolab.com/ C# Software Development Companies India
15. Like MVC, Web API supports Data Annotations
(System.ComponentModel.DataAnnotations)
Model Validation
http://www.ifourtechnolab.com/ C# Software Development Companies India
16. Model Validation - Example
http://www.ifourtechnolab.com/ C# Software Development Companies India
17. Create a FilterAttribute
Add it to Filters in Global.asax Application_Start
Model Validation - FilterAttribute
http://www.ifourtechnolab.com/ C# Software Development Companies India