Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
| Value | |
|---|---|
| Rule ID | RDG010 |
| Fix is breaking or non-breaking | Non-breaking |
Cause
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.
Rule description
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
{
}
How to fix violations
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
{
}
When to suppress warnings
This warning should not be suppressed. Suppressing the warning leads to a runtime exception associated with the same warning.
ASP.NET Core