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
The following `WeatherForecastController` logs the <xref:Microsoft.AspNetCore.Identity.IdentityUser%601.UserName> when the `Get` method is called.
31
31
32
32
> [!NOTE]
33
-
> The following example uses a [file-scoped namespace](/dotnet/csharp/language-reference/keywords/namespace), which is a C# 10 or later (.NET 6 or later) feature.
33
+
> The following example uses:
34
+
>
35
+
> * A [file-scoped namespace](/dotnet/csharp/language-reference/keywords/namespace), which is a C# 10 or later (.NET 6 or later) feature.
36
+
> * A [primary constructor](/dotnet/csharp/whats-new/tutorials/primary-constructors), which is a C# 12 or later (.NET 8 or later) feature.
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/aot/request-delegate-generator/rdg.md
+22-3Lines changed: 22 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,4 @@
1
1
---
2
-
3
2
title: ASP.NET Core Request Delegate Generator (RDG) for Native AOT
4
3
description: Turn Map methods into request delegates with the ASP.NET Core Request Delegate Generator (RDG) for Native AOT.
5
4
author: rick-anderson
@@ -48,7 +47,17 @@ The RDG:
48
47
* Is enabled automatically in projects when publishing with Native AOT is enabled or when trimming is enabled.
49
48
* Can be manually enabled even when not using Native AOT by setting `<EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>` in the project file:
@@ -57,7 +66,17 @@ Manually enabling RDG can be useful for:
57
66
58
67
Minimal APIs are optimized for using <xref:System.Text.Json>, which requires using the [System.Text.Json source generator](/dotnet/standard/serialization/system-text-json/source-generation). All types accepted as parameters to or returned from request delegates in Minimal APIs must be configured on a <xref:System.Text.Json.Serialization.JsonSerializerContext> that's registered via ASP.NET Core's dependency injection:
The [`Microsoft.AspNetCore.OpenApi`](https://www.nuget.org/packages/Microsoft.AspNetCore.OpenApi) package provides built-in support for OpenAPI document generation in ASP.NET Core. The package provides the following features:
16
16
17
-
* Support for generating OpenAPI documents at run time and accessing them via an endpoint on the application.
17
+
* Support for generating OpenAPI documents at run time and accessing them via an endpoint on the app.
18
18
* Support for "transformer" APIs that allow modifying the generated document.
19
19
* Support for generating multiple OpenAPI documents from a single app.
20
20
* Takes advantage of JSON schema support provided by [`System.Text.Json`](/dotnet/api/system.text.json).
@@ -112,7 +112,7 @@ The OpenAPI endpoint doesn't enable any authorization checks by default. Howeve
112
112
113
113
#### Cache generated OpenAPI document
114
114
115
-
The OpenAPI document is regenerated every time a request to the OpenAPI endpoint is sent. Regeneration enables transformers to incorporate dynamic application state into their operation. For example, regenerating a request with details of the HTTP context. When applicable, the OpenAPI document can be cached to avoid executing the document generation pipeline on each HTTP request.
115
+
The OpenAPI document is regenerated every time a request to the OpenAPI endpoint is sent. Regeneration enables transformers to incorporate dynamic app state into their operation. For example, regenerating a request with details of the HTTP context. When applicable, the OpenAPI document can be cached to avoid executing the document generation pipeline on each HTTP request.
@@ -122,7 +122,7 @@ In some scenarios, it's helpful to generate multiple OpenAPI documents with diff
122
122
123
123
* Generating OpenAPI documentation for different audiences, such as public and internal APIs.
124
124
* Generating OpenAPI documentation for different versions of an API.
125
-
* Generating OpenAPI documentation for different parts of an application, such as a frontend and backend API.
125
+
* Generating OpenAPI documentation for different parts of an app, such as a frontend and backend API.
126
126
127
127
To generate multiple OpenAPI documents, call the <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> extension method once for each document, specifying a different document name in the first parameter each time.
128
128
@@ -135,9 +135,12 @@ Each invocation of <xref:Microsoft.Extensions.DependencyInjection.OpenApiService
135
135
136
136
The framework uses the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method of <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions> to determine which endpoints to include in each document.
137
137
138
-
For each document, the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method is called for each endpoint in the application, passing the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object for the endpoint. The method returns a boolean value indicating whether the endpoint should be included in the document. The <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object contains information about the endpoint, such as the HTTP method, route, and response types, as well as metadata attached to the endpoint via attributes or extension methods.
138
+
For each document, the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method is called for each endpoint in the app, passing the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object for the endpoint. The method returns a boolean value indicating whether the endpoint should be included in the document. The <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object:
139
139
140
-
The default implementation of this delegate uses the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription.GroupName> field of <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription>, which is set on an endpoint using either the <xref:Microsoft.AspNetCore.Builder.RoutingEndpointConventionBuilderExtensions.WithGroupName%2A> extension method or the <xref:Microsoft.AspNetCore.Routing.EndpointGroupNameAttribute> attribute, to determine which endpoints to include in the document. Any endpoint that has not been assigned a group name is included all OpenAPI documents.
140
+
* contains information about the endpoint, such as the HTTP method, route, and response types
141
+
* Metadata attached to the endpoint via attributes or extension methods.
142
+
143
+
The default implementation of this delegate uses the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription.GroupName> field of <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription>. The delegate is set on an endpoint using either the <xref:Microsoft.AspNetCore.Builder.RoutingEndpointConventionBuilderExtensions.WithGroupName%2A> extension method or the <xref:Microsoft.AspNetCore.Routing.EndpointGroupNameAttribute> attribute. `WithGroupName` or the `EndpointGroupName` attribute determines which endpoints to include in the document. Any endpoint that has not been assigned a group name is included all OpenAPI documents.
141
144
142
145
```csharp
143
146
// Include endpoints without a group name or with a group name that matches the document name
@@ -148,13 +151,13 @@ You can customize the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldIn
148
151
149
152
## Generate OpenAPI documents at build-time
150
153
151
-
In typical web applications, OpenAPI documents are generated at run-time and served via an HTTP request to the application server.
154
+
In typical web apps, OpenAPI documents are generated at run-time and served via an HTTP request to the app server.
152
155
153
-
In some scenarios, it's helpful to generate the OpenAPI document during the application's build step. These scenarios include:
156
+
In some scenarios, it's helpful to generate the OpenAPI document during the app's build step. These scenarios include:
154
157
155
-
- Generating OpenAPI documentation that is committed into source control.
156
-
- Generating OpenAPI documentation that is used for spec-based integration testing.
157
-
- Generating OpenAPI documentation that is served statically from the web server.
158
+
* Generating OpenAPI documentation that is committed into source control.
159
+
* Generating OpenAPI documentation that is used for spec-based integration testing.
160
+
* Generating OpenAPI documentation that is served statically from the web server.
158
161
159
162
To add support for generating OpenAPI documents at build time, install the `Microsoft.Extensions.ApiDescription.Server` package:
160
163
@@ -173,20 +176,37 @@ Run the following command in the directory that contains the project file:
Upon installation, this package will automatically generate the Open API document(s) associated with the application during build and populate them into the application's output directory.
182
+
Upon installation, this package:
183
+
184
+
* Automatically generates the Open API document(s) associated with the app during build.
185
+
* Populates the Open API documents in the app's output directory.
186
+
187
+
If multiple documents are registered, ***and*** document name is ***not***`v1`, it's post-fixed with the document name. E.g., `{ProjectName}_{DocumentName}.json`.
188
+
189
+
# [Visual Studio Code](#tab/visual-studio-code)
190
+
191
+
```powershell
192
+
dotnet build
193
+
type obj\{ProjectName}.json
194
+
```
195
+
196
+
# [.NET Core CLI](#tab/netcore-cli)
179
197
180
198
```cli
181
-
$ dotnet build
182
-
$ cat bin/Debug/net9.0/{ProjectName}.json
199
+
dotnet build
200
+
cat obj/{ProjectName}.json
183
201
```
184
202
203
+
---
204
+
185
205
### Customizing build-time document generation
186
206
187
207
#### Modifying the output directory of the generated Open API file
188
208
189
-
By default, the generated OpenAPI document will be emitted to the application's output directory. To modify the location of the emitted file, set the target path in the `OpenApiDocumentsDirectory` property.
209
+
By default, the generated OpenAPI document will be emitted to the app's output directory. To modify the location of the emitted file, set the target path in the `OpenApiDocumentsDirectory` property.
190
210
191
211
```xml
192
212
<PropertyGroup>
@@ -198,7 +218,7 @@ The value of `OpenApiDocumentsDirectory` is resolved relative to the project fil
198
218
199
219
#### Modifying the output file name
200
220
201
-
By default, the generated OpenAPI document will have the same name as the application's project file. To modify the name of the emitted file, set the `--file-name` argument in the `OpenApiGenerateDocumentsOptions` property.
221
+
By default, the generated OpenAPI document will have the same name as the app's project file. To modify the name of the emitted file, set the `--file-name` argument in the `OpenApiGenerateDocumentsOptions` property.
202
222
203
223
```xml
204
224
<PropertyGroup>
@@ -208,7 +228,7 @@ By default, the generated OpenAPI document will have the same name as the applic
208
228
209
229
#### Selecting the OpenAPI document to generate
210
230
211
-
Some applications may be configured to emit multiple OpenAPI documents, for various versions of an API or to distinguish between public and internal APIs. By default, the build-time document generator will emit files for all documents that are configured in an application. To only emit for a single document name, set the `--document-name` argument in the `OpenApiGenerateDocumentsOptions` property.
231
+
Some apps may be configured to emit multiple OpenAPI documents. Multiple OpenAPI documents may be generated for different versions of an API or to distinguish between public and internal APIs. By default, the build-time document generator emits files for all documents that are configured in an app. To only emit for a single document name, set the `--document-name` argument in the `OpenApiGenerateDocumentsOptions` property.
0 commit comments