My API’s are usually exposed using what I call controlling classes. These classes define different API methods.
Sample class
Here’s an example of a controlling class
1 | using Microsoft.AspNetCore.Mvc; |
The Route and ApiContrller statements are called Annotation’s. You add Annotations to classes, or methods as a way to add functionality to your code without adding the code itself.
Route defines the URL path to access methods in the class.
ApiController is used to tell the compiler that the class is an API controller. When you use the ApiController attribute, you are required to add the word Controller to the class name as a suffix.
‘public class ReconcilesController’ is the class name. The methods in this class can be reached with something like http://localhost:5000/api/v1/reconciles
1 | private readonly IReconcileService _reconcileService; |
Public ReconcilesController(…) is executed each time the class is instantiated. When the class is instantiated, a property called Dependency Injection will inject (IReconcileService reconcileService) a class which was created that implements the IReconcileService interface.
We store a reference of the class being passed in into the private variable _reconcileService.
1 | [] |
This method returns records from the database. It does this by calling the GetAllReconcilesAsync() method in the class that implements the IReconcileService interface. it returns a List object loaded with ReconcileViewModel objects. This is wrapped in a IResult object..
[HttpGet] is an annotation that tells the compiler that the method is a method that can be called via an HTTP GET request.
There’s a few other method related annotations [HttpPost], [HttpPut], [HttpDelete]. These are used to tell the compiler that the method is a method that can be called via an HTTP POST, PUT, or DELETE request. This is somewhat of a tricky concept, but it’s important to understand. Suffice to say browsers can send HTTP GET, POST, PUT, and DELETE requests to API’s. Programs like Postman can also send HTTP GET, POST, PUT, and DELETE requests to API’s.
1 | [] |
You can configure the Get (post, put, and delete statements) so that they accept multiple parameters. In this particular example, the method GetOne() accepts a single parameter called id. The id parameter is a long data type. The id parameter is passed in as a parameter to the method. The id parameter is used to retrieve a record from the database.
You could enter a command like this in a browser: http://localhost:5000/api/v1/reconciles/1 and it will return a record from the database.
To pass 2 properties to a method, you can use the following syntax:
1 | [] |
In this case 2 arguments are expected. They are pulled out of the URL and passed to the GetByBankAccountNdStartDate() method. The first argument going into the bankAccountId parameter and the second argument going into the startDate parameter.
Swagger
Open API is a specification for describing, producing, consuming, and visualizing RESTful web services. Years ago Open API was called Swagger. Maybe Swagger started getting popular and renamed part of it’s technologies to Open API.
Swagger is a tool that helps you document your API’s.
You would add different different stuff to your methods to document your API’s.
Here’s an example
1 | /// <summary> |
You’ll notice a mix of comments (that start with ///) and Annotations. The compiler will call a function to read through the code and generate documentation. In this case the documentation that is generated will give you a summary and a description of the ID parameter. There is also comments that describe what is returned.
More information about Swagger can be found here:
- Swashbuckle There are articles in this section that describe different aspects of Swashbuckle.
More information about API can be found here: