You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/minimal-apis/includes/middleware8.md
+20-12Lines changed: 20 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,17 @@
1
1
:::moniker range=">= aspnetcore-8.0"
2
2
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
+
9
15
* User configured middleware and endpoints are added between `UseRouting` and `UseEndpoints`.
10
16
11
17
The following code is effectively what the automatic middleware being added to the app produces:
@@ -28,10 +34,10 @@ if (isAuthorizationConfigured)
28
34
app.UseAuthorization();
29
35
}
30
36
31
-
//user middleware/endpoints
37
+
//User middleware/endpoints
32
38
app.CustomMiddleware(...);
33
39
app.MapGet("/", () =>"hello world");
34
-
//end user middleware/endpoints
40
+
//End user middleware/endpoints
35
41
36
42
app.UseEndpoints(e=> {});
37
43
```
@@ -44,7 +50,7 @@ app.UseAuthentication();
44
50
app.UseAuthorization();
45
51
```
46
52
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:
48
54
49
55
```csharp
50
56
app.Use((context, next) =>
@@ -54,13 +60,15 @@ app.Use((context, next) =>
54
60
55
61
app.UseRouting();
56
62
57
-
//other middleware and endpoints
63
+
//Other middleware and endpoints
58
64
```
59
65
60
66
When adding a terminal middleware:
61
67
62
68
* 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
+
64
72
```csharp
65
73
app.UseRouting();
66
74
@@ -77,6 +85,6 @@ app.Run(context =>
77
85
78
86
Terminal middleware is middleware that runs if no endpoint handles the request.
79
87
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>.
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/minimal-apis/includes/route-handlers.md
+27-22Lines changed: 27 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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.
2
4
3
5
### Lambda expression
4
6
@@ -16,58 +18,61 @@ Route handlers are methods that execute when the route matches. Route handlers c
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:
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.
40
42
41
-
**NOTE**: Endpoint names are case sensitive.
43
+
#### Criteria for endpoint names
42
44
43
-
Endpoint names:
45
+
Endpoint names must satisfy the following criteria:
44
46
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).
47
50
48
-
### Route Parameters
51
+
### Route parameters
49
52
50
53
Route parameters can be captured as part of the route pattern definition:
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`.
55
58
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 typesafe 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`.
57
60
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:
59
62
60
-
**`BadHttpRequestException: Failed to bind parameter "int userId" from "hello".`**
63
+
```output
64
+
BadHttpRequestException: Failed to bind parameter "int userId" from "hello".
65
+
```
61
66
62
67
### Wildcard and catch all routes
63
68
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:
description: Use middleware in Minimal API applications
4
+
description: Use middleware in Minimal API applications, including automatic middleware, user-configured middleware, and terminal middleware.
5
5
ms.author: wpickett
6
-
ms.date: 02/16/2024
6
+
ms.date: 04/28/2026
7
7
monikerRange: '>= aspnetcore-7.0'
8
8
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.
9
11
---
10
12
11
13
# Middleware in Minimal API apps
12
14
13
15
[!INCLUDE[](~/includes/not-latest-version.md)]
14
16
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.
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
19
25
20
-
For more information about Minimal APIs see [APIs overview](xref:fundamentals/apis).
0 commit comments