Day1 Presentation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

ASP.

Net Web API


Building HTTP based Web Services

Mohamed ELshafei
HELLO!
What is API ?

Application Programing Interface.


API is some kind of interface which has a set of functions that allow
programmers to access specific features or data of an application,
operating system or other services.
Web API as the name suggests, is an API over the web which can be
accessed using HTTP protocol. It is a concept and not a technology.
We can build Web API using different technologies such as Java, .NET
etc
ASP.NET Web API

A framework that makes it easy to build HTTP services that reach a


broad range of clients, including browsers and mobile devices.
Clients:
ASP.NET Ecosystem
ASP.NET Web API Characteristics

ASP.NET Web API is an ideal platform for building RESTful services.


ASP.NET Web API is built on top of ASP.NET and supports ASP.NET
request/response pipeline
ASP.NET Web API maps HTTP verbs to method names.
Support Data Format : JSON, XML,BSON and Custom.
Support all clients are based on HTTP.
Combine ASP.NET.
HTTP Method

Method Objection

GET Retrieves information from a resource. return 200 (OK)

POST Requests the server to create new one of entity without duplicate object.
Return code 201 (Created)
PUT Requests the server to replace the state of the target resource at the
specified URI with the enclosed entity. If an existing resource exists for the
current representation, it should return a 200(OK) ,204 (No Content) ,201
(Created).
DELETE Requests the server to remove the entity located at the specified URI. Return
code 200(completed) or 204 (No Content).
HTTP Methods

Operation HTTP Method


Create POST
Read GET
Update PUT
Delete DELETE
ASP.NET Web API

Uses common concepts from ASP.NET MVC


Controllers
Routing
Model Binding
Model Validation
Security
Routing a Web API
Routing

Routing table

Routing by Action
Routing

Attribute Routing
 Route Attribute
 Route Prefix
HTTP Status Codes

Identify the type of request success and failures


 2xx – Successful Response
201 - Created

 3xx – Redirect Response


301 – Moved Permanently

 4xx – Client Error Response


401 – Unauthorized

 5xx – Service Error Response


503 – Service Unavailable
HTTP Status Codes

 201 Created
 200 Success - ok
 204 Success but No Content
 401 Not authorized
 404 Does not exist
 400 Bad Request
 500 Server Error
Action Results

void
return empty status 204 (No content)
HttpResponseMessage
convert directly to HTTP response message
Action Results

IHttpActionResult “Web API 2”


create HttpResponseMessage, then convert to HTTP response message

Other
write serialized value into response body with status 200 (OK)
HTTP Headers

Provide valuable meta data for the message


Some Standard Headers include:
Accept – Accepted formats by the Client
Content-Type – Format associated with the request body
Host – Server domain name
User-Agent – Client software identifier
Help Page

it is often useful to create a help page, so that other developers will know
how to call your API.
You can add help pages to an existing Web API project by using NuGet
Package Manager.
For a C# application: Install-Package Microsoft.AspNet.WebApi.HelpPage
protected void Application_Start()
{
// Add this code, if not present.
AreaRegistration.RegisterAllAreas();

// ...
}
Exception Handling

What happens if a Web API controller throws an uncaught exception?


By default, most exceptions are translated into an HTTP response with
status code 500, Internal Server Error.
The HttpResponseException type is a special case. This exception
returns any HTTP status code that you specify in the exception
constructor.
Exception Handling

you can also construct the entire response message and include it with
the HttpResponseException:
HttpError

The HttpError object provides a consistent way to return error


information in the response body.
Media Formatters

A media type, also called a MIME type, identifies the format of a piece of
data.
In HTTP, media types describe the format of the message body. A media
type consists of two strings, a type and a subtype. For example: text/html
The Content-Type header specifies the format of the message body.This
tells the receiver how to parse the contents of the message body.
The Accept header tells the server which media type(s) the client wants
from the server.
Accept: text/html,application/xhtml+xml,application/xml
Validation Attributes

Metadata Validation Attributes:


 [Required]
 [StringLength(60)]
 [Exclude]
 [RegularExpression]
 [DataType]  [AllowHtml]
 [Range]  [Compare]

[MetadataTypeAttribute( typeof( Employee.EmployeeMetadata ) )]


public partial class Employee
{
internal sealed class EmployeeMetadata
{
[StringLength(60)]
[RoundtripOriginal]
public string AddressLine { get; set; }
}
}
JSON and XML Serialization

In ASP.NET Web API, a media-type formatter is an object that can:


Read CLR objects from an HTTP message body
Write CLR objects into an HTTP message body
Web API provides media-type formatters for both JSON and XML. The
framework inserts these formatters into the pipeline by default. Clients
can request either JSON or XML in the Accept header of the HTTP
request.

You might also like