API Engineering & Cloud Computing
Yin Hua Li
Centennial College
Week#8
Topic: Web API
1
¡ Web API offers a simple communication
technology based on Representational State
Transfer (REST)
2
3
4
5
6
7
¡ Add data model
8
¡ Install NuGet Package
¡ Create Database Context class
9
¡ Register the database context
10
11
12
Object
ControllerBase
Controller
ControllerBase:
a base class for a
MVC controller
without view support
13
14
¡ The OpenAPI specification(here) is a language-
agnostic specification for machine-readable
interface files for describing, producing,
consuming and visualizing RESTful API
¡ OpenAPI allows both computer and humans to
understand the capabilities of a service without
any direct access to the implementation (source
code, network access, documentation)
15
¡ RESTful API metadata includes 5 parts
§ Resource description
▪ Resource refers to the information returned by an API
§ Endpoints and methods
▪ The endpoints indicate how you access the resource, and the
method used with the endpoint indicates the allowed
interactions (i.e., GET, POST, PUT or DELETE) with the
resource
§ Parameters
§ Request example
§ Response example
16
¡ Web API routing matches the URI of a HTTP request to a
particular action (method) in an API controller.
¡ There are two ways of routing
§ Convention-based routing
§ Attribute-based routing (recommended for Web API)
▪ Place [Route] on the controller or the action
▪ The [HttpGet] attribute denotes a method that responds to an HTTP GET
request. If no [Route] attribute is provided for a method, the URL path for
this method is constructed as following
▪ Take the template string provided by the controller’s [Route] attribute
▪ Replace [controller] with the name of the controller which is the controller class name
excluding the “Controller” suffix. E.g., for TodoItemsController, “TodoItems” should
be used to replace [controller] in [Route] template
17
¡ Please note that
§ Route is used at controller level to provide a template that will
prefix all templates defined at action level
18
19
20
¡ Postman is currently one of the most popular tools used in API testing
¡ Go to https://www.getpostman.com/downloads/ and choose your desired
platform
21
22
23
24
¡ https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-
6.0&tabs=visual-studio
¡ https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-
in-aspnet-web-api
25