With minimalistic API how does MapGet automatically fill parameters from querystring?
With minimalistic API the following is possible:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("api/Students/Grades", StudentsDataContext.GetGradesAsync).RequireAuthorization("Admin");
//...
public class Grade
{
public string? Subject { get; set; }
public int GradePercentage { get; set; } = 0;
}
public class StudentsDataContext
{
public static async Task<List<Grade>> GetGradesAsync(int? studentId, ClaimsPrincipal user, CancellationToken ct))
{
// Gets grades from database...
return new List<Grade>() {
new () { Subject = "Algebra", GradePercentage=95 },
new () { Subject = "English", GradePercentage=90 }
};
}
}
When you call: /api/Students/Grades?studentId=5
magically, studentId is passed to the GetGradesAsync, as well as ClaimsPrinicipal, and CancellationToken.
How does this witchcraft work? Is it possible to learn this power of the darkside?