Skip to main content

Extensions

The primary goal of this namespace is to enhance the developer experience by providing convenient "shortcut" and "setup" methods. These extension methods reduce boilerplate code and simplify common API development tasks in ASP.NET Core. They abstract away setup complexity and provide elegant shortcuts, guiding developers toward writing cleaner, more organized, and more maintainable API code.

The namespace contains several key classes, each serving a distinct purpose:

1. HttpEndpointServiceCollectionExtensions​

This class is the engine behind your Endpoint Registration feature. Its purpose is to declutter your Program.cs file by discovering and mapping API endpoints at compile time through the source generator shipped with the package. This is a core pattern for implementing a clean Vertical Slice Architecture.

2. ValidationExtensions​

This class is designed to improve the developer experience when applying validation to Minimal API endpoints. It provides a more fluent and readable syntax for adding the ValidationEndpointFilter.

3. ApplicationBuilderExtensions​

This class provides the Use (pipeline registration) method for every middleware shipped with the package:

  • UseRaExceptionHandling() — adds the GlobalExceptionHandler middleware to the pipeline, ensuring every unhandled exception is logged and returned to the client as a structured error response.
  • UseDefaultHeadersMiddleware() — adds the DefaultHeadersMiddleware to the pipeline, enforcing the required headers configured in DefaultHeadersOptions.RequiredHeaders.
  • UseLoggingMiddleware() — adds the LoggingMiddleware for structured HTTP request/response logging.
  • UseContextLoggingMiddleware() — adds the ContextLoggingMiddleware, which enriches log entries with request-scoped context such as the correlation ID. (The middleware type was renamed from RequestContextLoggingMiddleware in v10.2.1, and its Use method with it — AddRequestContextLoggingMiddleware() is unchanged.)

4. ServiceCollectionExtensions​

This class provides the matching Add (service registration) methods — each Use method above must be preceded by its counterpart here:

  • AddRaExceptionHandling() — registers the GlobalExceptionHandler with the dependency injection container.
  • AddDefaultHeadersMiddleware() — registers the DefaultHeadersMiddleware and configures DefaultHeadersOptions.
  • AddLoggingMiddleware() — registers the LoggingMiddleware and configures HttpLoggingOptions.
  • AddRequestContextLoggingMiddleware() — registers the ContextLoggingMiddleware with the dependency injection container.
v10.2.2

The former MiddlewareExtensions class was split into ApplicationBuilderExtensions (Use methods) and ServiceCollectionExtensions (Add methods). All extension methods keep their names — only code that references the class name directly needs updating. See the migration guide for details.