Back to Aspnetcore

ASP0015: Suggest using IHeaderDictionary properties

aspnetcore/diagnostics/asp0015.md

latest1.2 KB
Original Source

ASP0015: Suggest using IHeaderDictionary properties

Value
Rule IDASP0015
CategoryUsage
Fix is breaking or non-breakingNon-breaking

Cause

IHeaderDictionary properties are the recommended strategy for accessing headers.

Rule description

IHeaderDictionary properties are recommended for accessing headers. Accessing headers using an indexer as in the example below is not recommended.

csharp
var app = WebApplication.Create();

app.MapGet("/", (HttpContext context) => context.Request.Headers[""content-type""]);

app.Run();

How to fix violations

To fix a violation of this rule, use the property specified in the analyzer message to access the header specified in the message or apply the associated codefix.

csharp
var app = WebApplication.Create();
app.MapGet("/", (HttpContext context) => context.Request.Headers.ContentType);
app.Run();

When to suppress warnings

Do not suppress a warning from this rule.