Skip to content

[Analyzer] Header dictionary indexer analyzer and fixer #44318

@JamesNK

Description

@JamesNK

Background and Motivation

IHeaderDictionary has DIM properties for many commonly used headers: https://github.com/dotnet/aspnetcore/blob/ff2148be7592be83f1f88e2f372ef04f02fdfdb8/src/Http/Http.Features/src/IHeaderDictionary.Keyed.cs

These properties are better because:

  • Strongly typed so no chance of typos.
  • Faster because they directly fetch the known header value from the underlying collection.

Proposed API

An analyzer that looks for indexer use with a name that matches a property on IHeaderDictionary. This will only apply to header names that have a property, e.g.

var accepts = context.Request.Headers["accepts"]; // <- valid to fix
var id = context.Request.Headers["contoso-id"]; // <- ignored

Usage Examples

image

Alternative Designs

Risks

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implemented

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions