Add the Get Endpoint
The Get endpoint is generally the easiest and is a common endpoint to start with.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
// ✅ We'll add the endpoint here
app.Run();
Start with a simple inline endpoint
app.MapGet("/api/tasks", async () =>
{
return Results.Ok("It works!");
});
You can now run the application and check that it works using Postman or the endpoint functionality within Rider.
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()
public class TaskDto
{
public int Id { get; set; }
public string Title { get; set; } = default!;
public string? Description { get; set; }
public DateTime? DueDate { get; set; }
public bool Completed { get; set; }
}
First delete the previous test return
app.MapGet("/api/tasks", async () =>
{
});
We need to create a new connection to our database using SqlConnection and pass in our connection string
await using var connection = new SqlConnection(connectionString);
And then open the connection
await connection.OpenAsync();
Now we need to get the results
var tasks = await connection.QueryAsync<TaskDto>(
"GetTasks",
commandType: CommandType.StoredProcedure
);
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.
return Results.Ok(tasks);
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
using System.Data;
using Dapper;
using Microsoft.Data.SqlClient;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.MapGet("/api/tasks", async () =>
{
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var tasks = await connection.QueryAsync<TaskDto>(
"GetTasks",
commandType: CommandType.StoredProcedure
);
return Results.Ok(tasks);
});
app.Run();
public class TaskDto
{
public int Id { get; set; }
public string Title { get; set; } = default!;
public string? Description { get; set; }
public DateTime? DueDate { get; set; }
public bool Completed { get; set; }
}