Move snippets to project file /5 (#30490)

* Move snippets to project file /5

* Move snippets to project file /5
pull/30491/head
Rick Anderson 2023-09-26 17:08:41 -10:00 committed by GitHub
parent 2d3ff2624e
commit 39285ee11e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 28 deletions

View File

@ -16,43 +16,22 @@ uid: fundamentals/aot/request-delegate-generator/diagnostics/rdg002
## Cause
This diagnostic is emitted by the Request Delegate Generator when an endpoint contains a route handler that cannot be statically analyzed.
This diagnostic is emitted by the Request Delegate Generator when an endpoint contains a route handler that can't be statically analyzed.
### Rule description
The Request Delegate Generator runs at compile-time and needs to be able to statically analyze route handlers in an application. The implementation currently only supports route handlers that are provided as lambda expression, method group references, or references to read-only fields or variables.
The Request Delegate Generator runs at compile-time and needs to be able to statically analyze route handlers in an app. The current implementation only supports route handlers that are provided as a lambda expression, method group references, or references to read-only fields or variables.
```razor
var app = WebApplication.Create();
The following code generates the RDG002 warning because the route handler is provided as a reference to a method:
var del = Wrapper.GetTodos;
app.MapGet("/v1/todos", del);
app.Run();
record Todo(int Id, string Task);
class Wrapper
{
public static Func<IResult> GetTodos = ()
=> Results.Ok((List<Todo>)[new Todo(1, "Write tests"), new Todo(2, "Fix tests")]);
}
```
:::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/aot/diagonstics/Rdg2/Program.cs" id="snippet_1" highlight="13-14,25-00":::
## How to fix violations
Declare the route handler using supported syntax, such as an inline lambda.
```razor
var app = WebApplication.Create();
Declare the route handler using supported syntax, such as an inline lambda:
app.MapGet("/v1/todos", ()
=> Results.Ok((List<Todo>)[new Todo(1, "Write tests"), new Todo(2, "Fix tests")]));
app.Run();
record Todo(int Id, string Task);
```
:::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/aot/diagonstics/Rdg2/Program.cs" id="snippet_1f" highlight="13":::
## When to suppress warnings
This warning can be safely suppressed. When suppressed, the framework will fallback to generating the request delegate at runtime.
This warning can be safely suppressed. When suppressed, the framework falls back to generating the request delegate at runtime.