Skip to content

Commit 40fefba

Browse files
authored
Merge pull request #37067 from dotnet/main
Merge to Live
2 parents 07eef91 + 751043c commit 40fefba

12 files changed

Lines changed: 259 additions & 200 deletions

File tree

aspnetcore/fundamentals/minimal-apis/includes/middleware8.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
:::moniker range=">= aspnetcore-8.0"
22

3-
[`WebApplication`](xref:fundamentals/minimal-apis/webapplication) automatically adds the following middleware in [Minimal API applications](xref:fundamentals/apis) depending on certain conditions:
4-
* [`UseDeveloperExceptionPage`](/dotnet/api/microsoft.aspnetcore.diagnostics.developerexceptionpagemiddleware) is added first when the [`HostingEnvironment`](xref:fundamentals/environments) is `"Development"`.
5-
* [`UseRouting`](/dotnet/api/microsoft.aspnetcore.builder.endpointroutingapplicationbuilderextensions.userouting) is added second if user code didn't already call `UseRouting` and if there are endpoints configured, for example `app.MapGet`.
6-
* [`UseEndpoints`](/dotnet/api/microsoft.aspnetcore.builder.endpointroutingapplicationbuilderextensions.useendpoints) is added at the end of the middleware pipeline if any endpoints are configured.
7-
* [`UseAuthentication`](/dotnet/api/microsoft.aspnetcore.builder.authappbuilderextensions.useauthentication) is added immediately after `UseRouting` if user code didn't already call `UseAuthentication` and if [`IAuthenticationSchemeProvider`](/dotnet/api/microsoft.aspnetcore.authentication.iauthenticationschemeprovider) can be detected in the service provider. `IAuthenticationSchemeProvider` is added by default when using [`AddAuthentication`](/dotnet/api/microsoft.extensions.dependencyinjection.authenticationservicecollectionextensions.addauthentication), and services are detected using [`IServiceProviderIsService`](/dotnet/api/microsoft.extensions.dependencyinjection.iserviceproviderisservice).
8-
* [`UseAuthorization`](/dotnet/api/microsoft.aspnetcore.builder.authorizationappbuilderextensions.useauthorization) is added next if user code didn't already call `UseAuthorization` and if [`IAuthorizationHandlerProvider`](/dotnet/api/microsoft.aspnetcore.authorization.iauthorizationhandlerprovider) can be detected in the service provider. `IAuthorizationHandlerProvider` is added by default when using [`AddAuthorization`](/dotnet/api/microsoft.extensions.dependencyinjection.authenticationservicecollectionextensions.addauthentication), and services are detected using `IServiceProviderIsService`.
3+
[WebApplication](xref:fundamentals/minimal-apis/webapplication) automatically adds the following middleware in [Minimal API applications](xref:fundamentals/apis) depending on certain conditions:
4+
5+
* [UseDeveloperExceptionPage](/dotnet/api/microsoft.aspnetcore.diagnostics.developerexceptionpagemiddleware) is added first when the [HostingEnvironment](xref:fundamentals/environments) is `"Development"`.
6+
7+
* [UseRouting](/dotnet/api/microsoft.aspnetcore.builder.endpointroutingapplicationbuilderextensions.userouting) is added second, if the user code didn't already call `UseRouting` and endpoints are configured, for example `app.MapGet`.
8+
9+
* [UseEndpoints](/dotnet/api/microsoft.aspnetcore.builder.endpointroutingapplicationbuilderextensions.useendpoints) is added at the end of the middleware pipeline if endpoints are configured.
10+
11+
* [UseAuthentication](/dotnet/api/microsoft.aspnetcore.builder.authappbuilderextensions.useauthentication) is added immediately after `UseRouting`, if user code didn't already call `UseAuthentication` and if [IAuthenticationSchemeProvider](/dotnet/api/microsoft.aspnetcore.authentication.iauthenticationschemeprovider) can be detected in the service provider. `IAuthenticationSchemeProvider` is added by default when you use [AddAuthentication](/dotnet/api/microsoft.extensions.dependencyinjection.authenticationservicecollectionextensions.addauthentication), and services are detected by using [IServiceProviderIsService](/dotnet/api/microsoft.extensions.dependencyinjection.iserviceproviderisservice).
12+
13+
* [UseAuthorization](/dotnet/api/microsoft.aspnetcore.builder.authorizationappbuilderextensions.useauthorization) is added next, if user code didn't already call `UseAuthorization` and if [IAuthorizationHandlerProvider](/dotnet/api/microsoft.aspnetcore.authorization.iauthorizationhandlerprovider) can be detected in the service provider. `IAuthorizationHandlerProvider` is added by default when you use [AddAuthorization](/dotnet/api/microsoft.extensions.dependencyinjection.authenticationservicecollectionextensions.addauthentication), and services are detected by using `IServiceProviderIsService`.
14+
915
* User configured middleware and endpoints are added between `UseRouting` and `UseEndpoints`.
1016

1117
The following code is effectively what the automatic middleware being added to the app produces:
@@ -28,10 +34,10 @@ if (isAuthorizationConfigured)
2834
app.UseAuthorization();
2935
}
3036

31-
// user middleware/endpoints
37+
// User middleware/endpoints
3238
app.CustomMiddleware(...);
3339
app.MapGet("/", () => "hello world");
34-
// end user middleware/endpoints
40+
// End user middleware/endpoints
3541
3642
app.UseEndpoints(e => {});
3743
```
@@ -44,7 +50,7 @@ app.UseAuthentication();
4450
app.UseAuthorization();
4551
```
4652

47-
If middleware should be run before route matching occurs, <xref:Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseRouting%2A> should be called and the middleware should be placed before the call to `UseRouting`. <xref:Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints%2A> isn't required in this case as it is automatically added as described previously:
53+
If middleware should run before route matching occurs, <xref:Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseRouting%2A> should be called and the middleware should be placed before the call to `UseRouting`. <xref:Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints%2A> isn't required in this case because it's automatically added as described earlier:
4854

4955
```csharp
5056
app.Use((context, next) =>
@@ -54,13 +60,15 @@ app.Use((context, next) =>
5460

5561
app.UseRouting();
5662

57-
// other middleware and endpoints
63+
// Other middleware and endpoints
5864
```
5965

6066
When adding a terminal middleware:
6167

6268
* The middleware must be added after `UseEndpoints`.
63-
* The app needs to call `UseRouting` and `UseEndpoints` so that the terminal middleware can be placed at the correct location.
69+
70+
* The app needs to call `UseRouting` and `UseEndpoints` so the terminal middleware can be placed at the correct location.
71+
6472
```csharp
6573
app.UseRouting();
6674

@@ -77,6 +85,6 @@ app.Run(context =>
7785

7886
Terminal middleware is middleware that runs if no endpoint handles the request.
7987

80-
For information on antiforgery middleware in Minimal APIs, see <xref:security/anti-request-forgery#afwma>
88+
For information on antiforgery middleware in Minimal APIs, see <xref:security/anti-request-forgery#afwma>.
8189

8290
:::moniker-end

aspnetcore/fundamentals/minimal-apis/includes/route-handlers.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Route handlers are methods that execute when the route matches. Route handlers can be a lambda expression, a local function, an instance method or a static method. Route handlers can be synchronous or asynchronous.
1+
Route handlers are methods that execute when the route matches. Route handlers can be a lambda expression, a local function, an instance method, or a static method. Route handlers can be synchronous or asynchronous.
2+
3+
The following sections provide examples of different route handlers.
24

35
### Lambda expression
46

@@ -16,58 +18,61 @@ Route handlers are methods that execute when the route matches. Route handlers c
1618

1719
[!code-csharp[](~/fundamentals/minimal-apis/7.0-samples/WebMinAPIs/Program.cs?name=snippet_sm)]
1820

19-
### Endpoint defined outside of `Program.cs`
21+
### Endpoint defined outside of Program.cs
2022

21-
Minimal APIs don't have to be located in `Program.cs`.
23+
Minimal APIs don't have to be located in the _Program.cs_ file. For example, you can set up the structure in the _Program.cs_ file, and define the endpoint in a separate file:
2224

23-
`Program.cs`
25+
**Program.cs**
2426

2527
[!code-csharp[](~/fundamentals/minimal-apis/8.0-samples/MinAPISeparateFile/Program.cs)]
2628

27-
`TodoEndpoints.cs`
29+
**TodoEndpoints.cs**
2830

2931
[!code-csharp[](~/fundamentals/minimal-apis/8.0-samples/MinAPISeparateFile/TodoEndpoints.cs)]
3032

31-
See also [Route groups](#route-groups) later in this article.
33+
For more information, see the [Route groups](#route-groups) section later in this article.
3234

3335
### Named endpoints and link generation
3436

35-
Endpoints can be given names in order to generate URLs to the endpoint. Using a named endpoint avoids having to hard code paths in an app:
37+
You can supply a name for your endpoints to generate URLs that target the endpoint. Using a named endpoint avoids having to hard code paths in an app:
3638

3739
[!code-csharp[](~/fundamentals/minimal-apis/samples/WebMinAPIs/Program.cs?name=snippet_nr)]
3840

39-
The preceding code displays `The link to the hello route is /hello` from the `/` endpoint.
41+
The preceding code displays the message _`The link to the hello route is /hello`_ from the `/` (forward slash) endpoint.
4042

41-
**NOTE**: Endpoint names are case sensitive.
43+
#### Criteria for endpoint names
4244

43-
Endpoint names:
45+
Endpoint names must satisfy the following criteria:
4446

45-
* Must be globally unique.
46-
* Are used as the OpenAPI operation id when OpenAPI support is enabled. For more information, see [OpenAPI](xref:fundamentals/openapi/aspnetcore-openapi).
47+
* Endpoint names are case sensitive.
48+
* Endpoint names must be globally unique.
49+
* Endpoint names are used as the OpenAPI operation identifier (ID) when OpenAPI support is enabled. For more information, see [Generate OpenAPI documents](xref:fundamentals/openapi/aspnetcore-openapi).
4750

48-
### Route Parameters
51+
### Route parameters
4952

5053
Route parameters can be captured as part of the route pattern definition:
5154

5255
[!code-csharp[](~/fundamentals/minimal-apis/7.0-samples/WebMinAPIs/Program.cs?name=snippet_rp)]
5356

54-
The preceding code returns `The user id is 3 and book id is 7` from the URI `/users/3/books/7`.
57+
The preceding code returns the message _The user id is 3 and book id is 7_ from the URI `/users/3/books/7`.
5558

56-
The route handler can declare the parameters to capture. When a request is made to a route with parameters declared to capture, the parameters are parsed and passed to the handler. This makes it easy to capture the values in a type safe way. In the preceding code, `userId` and `bookId` are both `int`.
59+
The route handler can declare the parameters to capture. When a request is made to a route with parameters declared to capture, the parameters are parsed and passed to the handler. This approach makes it easy to capture the values in a type-safe way. In the preceding code, the `userId` and `bookId` parameters are both type `int`.
5760

58-
In the preceding code, if either route value cannot be converted to an `int`, an exception is thrown. The GET request `/users/hello/books/3` throws the following exception:
61+
In the preceding code, if either route value can't be converted to an `int`, an exception is thrown. The GET request `/users/hello/books/3` throws the following exception:
5962

60-
**`BadHttpRequestException: Failed to bind parameter "int userId" from "hello".`**
63+
```output
64+
BadHttpRequestException: Failed to bind parameter "int userId" from "hello".
65+
```
6166

6267
### Wildcard and catch all routes
6368

64-
The following catch all route returns `Routing to hello` from the `/posts/hello' endpoint:
69+
The following catch all route returns _Routing to hello_ from the `/posts/hello` endpoint:
6570

6671
[!code-csharp[](~/fundamentals/minimal-apis/7.0-samples/WebMinAPIs/Program.cs?name=snippet_wild)]
6772

6873
### Route constraints
6974

70-
Route constraints constrain the matching behavior of a route.
75+
Route constraints restrict the matching behavior of a route.
7176

7277
```csharp
7378
var builder = WebApplication.CreateBuilder(args);
@@ -82,13 +87,13 @@ app.Run();
8287

8388
The following table demonstrates the preceding route templates and their behavior:
8489

85-
| Route Template | Example Matching URI |
86-
|--|--|
90+
| Route template | Example matching URI |
91+
|---|---|
8792
| `/todos/{id:int}` | `/todos/1` |
8893
| `/todos/{text}` | `/todos/something` |
8994
| `/posts/{slug:regex(^[a-z0-9_-]+$)}` | `/posts/mypost` |
9095

91-
For more information, see [Route constraint reference](xref:fundamentals/routing) in <xref:fundamentals/routing>.
96+
For more information, see [Route constraint reference](xref:fundamentals/routing#route-constraints) in <xref:fundamentals/routing>.
9297

9398
### Route groups
9499

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
---
22
title: Middleware with Minimal API applications
33
author: BrennanConroy
4-
description: Use middleware in Minimal API applications
4+
description: Use middleware in Minimal API applications, including automatic middleware, user-configured middleware, and terminal middleware.
55
ms.author: wpickett
6-
ms.date: 02/16/2024
6+
ms.date: 04/28/2026
77
monikerRange: '>= aspnetcore-7.0'
88
uid: fundamentals/minimal-apis/middleware
9+
10+
# customer intent: As an ASP.NET developer, I want to use middleware in ASP.NET Core, so I can use the functionality to handle requests and responses in my Minimal API apps.
911
---
1012

1113
# Middleware in Minimal API apps
1214

1315
[!INCLUDE[](~/includes/not-latest-version.md)]
1416

17+
This article describes how to use middleware in Minimal API applications. Take advantage of automatic middleware in your Minimal API apps, or define user-configured middleware and terminal middleware.
18+
19+
## Available middleware
20+
1521
[!INCLUDE [webapplication7](~/fundamentals/minimal-apis/includes/middleware7.md)]
1622
[!INCLUDE [webapplication8](~/fundamentals/minimal-apis/includes/middleware8.md)]
1723

18-
For more information about middleware see [ASP.NET Core Middleware](xref:fundamentals/middleware/index), and the [list of built-in middleware](xref:fundamentals/middleware/index#built-in-middleware) that can be added to applications.
24+
## Related content
1925

20-
For more information about Minimal APIs see [APIs overview](xref:fundamentals/apis).
26+
- [ASP.NET Core Middleware](xref:fundamentals/middleware/index)
27+
- [Built-in middleware (list)](xref:fundamentals/middleware/index#built-in-middleware)
28+
- [Minimal APIs overview](xref:fundamentals/apis)

0 commit comments

Comments
 (0)