Detailed Notes on HTTP, REST, ASP.NET Core, and OWIN Middleware
Hypertext Transfer Protocol (HTTP)
Definition: A communications protocol allowing retrieval of inter-linked text documents (hypertext) on the World Wide Web.
HTTP Verbs: Important methods defining the actions that can be performed with HTTP requests. Key verbs include:
HEAD: Retrieves metadata without the body of the resource.
GET: Requests data from the server.
POST: Sends data to the server to create a resource.
PUT: Updates an existing resource.
DELETE: Removes a resource from the server.
TRACE, OPTIONS, CONNECT: Used for other specific interactions.
HTTP Request and Response Format
Request Format:
Components:
Request Line: Indicates the verb, resource, and HTTP version (e.g.,
GET /images/logo.gif HTTP/1.1).Headers: Additional information (e.g.,
Accept-Language: en).Body (optional): Contains data sent by the client.
Response Format:
Components:
Status Line: Contains the numeric status code and reason phrase (e.g.,
HTTP/1.1 200 OK).Response Headers: Provide metadata about the response.
Body: The actual content retrieved from the server.
HTTP Status Codes
Categories:
1XX: Informational responses.
2XX: Success; indicates successful requests.
3XX: Redirection; requests need further action.
4XX: Client errors; issues with the request.
5XX: Server errors; problems on the server side.
Representational State Transfer (REST)
Definition: An architectural style for developing web services that is resource-oriented rather than action-oriented.
Key Features:
Utilizes standard HTTP methods and URIs.
Information is organized as resources, identified by URIs.
Supports various media types for resource representations, such as JSON and XML.
Characteristics of a RESTful Network
Client-Server Architecture: Clients request resources as needed.
Stateless: Each request contains all necessary information; no session state on the server.
Cacheable: Responses can be labeled as cacheable to improve efficiency.
Uniform Interface: Use standard interfaces (e.g., HTTP methods) to interact with resources.
Resource Representation: Resources are interconnected using URIs to facilitate navigation.
Principles of RESTful Web Service Design
Identify conceptual entities as resources (e.g., parts list, orders).
Create unique URLs for each resource.
Differentiate between read-only (GET) and updatable (POST, PUT, DELETE) resources.
Ensure that GET requests are side-effect free.
Use hyperlinks within resource representations for related information.
Gradually reveal data rather than overwhelming clients with too much information at once.
Define response data formats using schemas.
Describe service invocation methods clearly.
ASP.NET Core RESTful Services
Framework Overview: Initially designed for building RESTful services without a view (MVC minus V).
Communication: Uses standard HTTP verbs and URIs for API calls.
ASP.NET Core HTTP Verbs
GET: Retrieves resource data.
POST: Creates new resources by sending data.
PUT: Updates existing resources.
DELETE: Removes resources.
Controller Actions with RESTful Services
HTTP status codes are essential for indicating success or failure in API responses.
Responses are typically formatted as JSON.
Problem Details for Error Status Codes
Errors (400 or higher) are transformed into a structured format for clarity and troubleshooting.
Open Web Interface for .NET (OWIN) and Middleware
Definition: OWIN decouples web applications from web servers, defining how middleware should handle requests.
ASP.NET Core Integration: Compatible libraries allow ASP.NET Core applications to work with OWIN middleware.
Running OWIN Middleware in ASP.NET Core
ASP.NET Core can implement OWIN middleware to extend functionality and support older applications.