Day1 Presentation
Day1 Presentation
Day1 Presentation
Mohamed ELshafei
HELLO!
What is API ?
Method Objection
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
Routing table
Routing by Action
Routing
Attribute Routing
Route Attribute
Route Prefix
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
Other
write serialized value into response body with status 200 (OK)
HTTP Headers
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
you can also construct the entire response message and include it with
the HttpResponseException:
HttpError
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