Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Valore | |
---|---|
Rule ID | RDG010 |
Fix is breaking or non-breaking | Non-breaking |
Causa
This diagnostic is emitted by the Request Delegate Generator when an endpoint contains a route handler with a parameter annotated with the [AsParameters]
attribute that is marked as nullable.
Descrizione della regola
The implementation of surrogate binding via the [AsParameters]
attribute in minimal APIs only supports types that are not nullable.
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0,
AppJsonSerializerContext.Default);
});
var app = builder.Build();
app.MapGet("/todos/{id}", ([AsParameters] TodoRequest? request)
=> Results.Ok(new Todo(request!.Id)));
app.Run();
public record TodoRequest(HttpContext HttpContext, [FromRoute] int Id);
public record Todo(int Id);
[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
Come correggere le violazioni
Declare the parameter as non-nullable.
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0,
AppJsonSerializerContext.Default);
});
var app = builder.Build();
app.MapGet("/todos/{id}", ([AsParameters] TodoRequest request)
=> Results.Ok(new Todo(request.Id)));
app.Run();
public record TodoRequest(HttpContext HttpContext, [FromRoute] int Id);
public record Todo(int Id);
[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
Quando eliminare gli avvisi
Questo avviso non deve essere soppresso. L'eliminazione dell'avviso comporta un'eccezione di runtime associata allo stesso avviso.