Welcome to the final lesson of our course on model binding in Minimal APIs! Throughout this course, we've explored what model binding is and how it seamlessly maps incoming HTTP request data to handler parameters. We've covered binding both simple and complex types, and making those parameters optional to build versatile and robust APIs. In this lesson, we'll focus on binding special types, such as HttpContext
, HttpRequest
, and others.
Let's start by delving into HttpContext
, an object created with every incoming request to your minimal API. It encapsulates all HTTP-specific information about the request. Alongside HttpContext
, several other related types like HttpRequest
, HttpResponse
, and ClaimsPrincipal
can also be directly bound.
Here is an example demonstrating how to bind and use HttpContext
and ClaimsPrincipal
:
C#1app.MapGet("/inspect", (HttpContext context, ClaimsPrincipal user) => 2{ 3 var info = new 4 { 5 Path = context.Request.Path, 6 User = user.Identity?.Name 7 }; 8 return Results.Json(info); 9});
In the code snippet above:
app.MapGet("/inspect", ...)
: Establishes a GET endpoint at/inspect
.HttpContext context
: Binds theHttpContext
to access request data and server info.ClaimsPrincipal user
: Binds the authenticated user's principal.context.Request.Path
: Extracts the path of the current request.user.Identity?.Name
: Retrieves the name of the authenticated user, if available.Results.Json(info)
: Returns the information as JSON.
This endpoint allows you to inspect essential request details like the path and the authenticated user.
Below is a table summarizing the different types that can be bound and their descriptions along with commonly used properties:
Type | Description | Commonly Used Properties |
---|---|---|
HttpContext | Comprehensive details about both the request and the response. It's the central hub for HTTP data. | Request , Response , User , Items , Features |
HttpRequest | Contains details specifically about the request, equivalent to HttpContext.Request . | Method , Path , Headers , Query , Body |
HttpResponse | Contains details specifically about the response, equivalent to HttpContext.Response . | StatusCode , Headers , Body , ContentType |
CancellationToken | Manages long-running tasks and is canceled if the client aborts the request, equivalent to HttpContext.RequestAborted . | - |
ClaimsPrincipal | Contains authentication information about the user, equivalent to HttpContext.User . | Identity , Claims , IsAuthenticated |
Stream | Refers to the request's body stream, useful for processing large amounts of data without holding it all in memory, equivalent to HttpRequest.Body . | - |
PipeReader | Provides a higher-level API compared to Stream , useful in similar scenarios, equivalent to HttpContext.BodyReader . | - |
Understanding how each type can be utilized along with their commonly used properties helps in efficiently handling various aspects of request processing. In the upcoming practices, we'll cover more obvious types like HttpContext
, HttpRequest
, and HttpResponse
.
In this lesson, you learned how to bind HttpContext
and other related types in minimal APIs. This concludes our course on model binding. Up next, get ready to practice and apply what you've learned. Happy coding!