Overview of REST
What is a web service?
When you browse to a webpage, the web server communicates with your browser by using HTML, CSS, and JavaScript. In a similar way, web servers can communicate with a broad range of clients (browsers, mobile devices, other web servers, and more) by using web services.
API clients communicate with the server over HTTP, and the two exchange information by using a data format such as JSON or XML. APIs are often used in single-page applications (SPAs) that perform most of the user-interface logic in a web browser. Communication with the web server primarily happens through web APIs.
What is REST API?
Representational State Transfer (REST) is an architectural style for building web services. REST requests are made over HTTP. They use the same HTTP verbs that web browsers use to retrieve webpages and send data to servers.
The verbs are:
GET
: Retrieve data from the web service.POST
: Create a new item of data on the web service.PUT
: Update an item of data on the web service.PATCH
: Update an item of data on the web service with specific instructions (not discussed here)DELETE
: Delete an item of data on the web service.
Web service APIs that adhere to REST are called RESTful APIs. They're defined through:
A base URI.
HTTP methods, such as
GET
,POST
,PUT
,PATCH
, orDELETE
.A media type for the data, such as JavaScript Object Notation (JSON) or XML.
Benefits of creating APIs in ASP.NET Core
With ASP.NET, you can use the same framework and patterns to build both webpages and services. This means you can reuse model classes and validation logic, and even serve both webpages and services side by side in the same project. This approach has benefits:
Simple serialization: ASP.NET was designed for modern web experiences. Endpoints automatically serialize your classes to properly formatted JSON out of the box.
Authentication and authorization: For security, API endpoints have built-in support for industry-standard JSON Web Tokens (JWTs).
Routing alongside your code: ASP.NET lets you define routes and verbs inline with your code by using attributes.
HTTPS by default: HTTPS is an important part of modern, professional web APIs. It relies on end-to-end encryption to provide privacy, and to help ensure that your API calls aren't intercepted and altered between client and server.
Last updated