Lesson 4
Binding Special Types
Introduction

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.

Binding HttpContext and Other Types

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 the HttpContext 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.

Available Common Types

Below is a table summarizing the different types that can be bound and their descriptions along with commonly used properties:

TypeDescriptionCommonly Used Properties
HttpContextComprehensive details about both the request and the response. It's the central hub for HTTP data.Request, Response, User, Items, Features
HttpRequestContains details specifically about the request, equivalent to HttpContext.Request.Method, Path, Headers, Query, Body
HttpResponseContains details specifically about the response, equivalent to HttpContext.Response.StatusCode, Headers, Body, ContentType
CancellationTokenManages long-running tasks and is canceled if the client aborts the request, equivalent to HttpContext.RequestAborted.-
ClaimsPrincipalContains authentication information about the user, equivalent to HttpContext.User.Identity, Claims, IsAuthenticated
StreamRefers to the request's body stream, useful for processing large amounts of data without holding it all in memory, equivalent to HttpRequest.Body.-
PipeReaderProvides 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.

Summary

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.