Date post: | 16-Apr-2017 |
Category: |
Software |
Upload: | ifour-institute-sustainable-learning |
View: | 85 times |
Download: | 0 times |
iFour Consultancy
ASP.NET MVC Web API
ASP.NET Web APIWhy ASP.NET MVC Web API?ASP.NET Web API FeaturesASP.NET Web API DesignASP.NET MVC Web APIRouting TableError & Exception HandlingModel Validation
INDEX
http://www.ifourtechnolab.com/
C# Software Development Companies India
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
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
Modern HTTP programming modelDirectly 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
FiltersWeb 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
Content negotiationThe client and server can work together to determine the right format for data being returned
from an APIProvide default support for XML, JSON, and Form URL-encoded formats, and extend this support
by adding your own formattersModel 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 detailsRather than setting HTTP details in static context objects, Web API actions can now work with
instances of HttpRequestMessage and HttpResponseMessageGeneric 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
Query compositionBy 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-hostWeb 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
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
MVC : Model -> View -> Controller Create a Model
Create a Controller
ASP.NET MVC Web API
http://www.ifourtechnolab.com/
C# Software Development Companies India
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
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 serverPUT updates a resource at a specified URIPOST Create a new resource. The server assigns the URI for the new object and returns this URI
as part of the response messageDELETE deletes a resource at a specified URI
Routing Table
http://www.ifourtechnolab.com/
C# Software Development Companies India
ASP.NET MVC Web API - Example
APIs for crud operation for products
http://www.ifourtechnolab.com/
C# Software Development Companies India
Response messages, errors and exceptions are translated to HTTP response status codesFor 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
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
Like MVC, Web API supports Data Annotations (System.ComponentModel.DataAnnotations)
Model Validation
http://www.ifourtechnolab.com/
C# Software Development Companies India
Model Validation - Example
http://www.ifourtechnolab.com/
C# Software Development Companies India
Create a FilterAttribute
Add it to Filters in Global.asax Application_Start
Model Validation - FilterAttribute
http://www.ifourtechnolab.com/
C# Software Development Companies India
https://www.tutorialspoint.com/asp.net_mvc/asp.net_mvc_web_api.htm https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-
your-first-web-api http://www.c-sharpcorner.com/uploadfile/4d9083/how-to-create-web-api-in-asp-net-
mvc/
References
http://www.ifourtechnolab.com/
C# Software Development Companies India
Questions?
http://www.ifourtechnolab.com/
C# Software Development Companies India