Add the Get Endpoint
The Get endpoint is generally the easiest and is a common endpoint to start with.
Start With a Simple Inline Endpoint
Start with a simple inline endpoint
You can now run the application and check that it works using Postman or the endpoint functionality within Rider.
Add a Model
First, we will create a DTO (Data Transfer Object) for our data.
A DTO is a model created with the specific purpose of data transfer, typically between the client and server. It contains only the data that needs to be transferred in a flat structure to reduce complexity in data transfer. DTOs are simple objects with only properties and no behaviour.
The DTO should contain all the properties required to represent our data
For the time being this will need to be placed at the bottom of the Program.cs file after app.Run()
Complete the Get Endpoint
First delete the previous test return
We need to create a new connection to our database using SqlConnection and pass in our connection string
And then open the connection
Now we need to get the results
This is where Dapper comes in. QueryAsync is a Dapper command that calls a SQL query or stored procedure and maps each row in the result to an object of type T — in this case, TaskDto.
Finally, we need to return the results. We will wrap the results in a Results.Ok method which will return an HTTP OK response. The list is automatically converted to JSON by .NET.
Some methods will be highlighted red as they need to use the Nuget package we added, so we need to import it into the file by adding a using statement. We can have Rider do this for us by clicking one of the highlighted methods and pressing option + return.
so our completed program.cs file and endpoint should look like this