Skip to content

Invalid nullability annotation on HttpRequest.ContentType #32097

Closed
@bart-degreed

Description

@bart-degreed

After updating my Web API project from .NET Core 3.1 to .NET 5, I'm getting a warning along the lines of "variable is never null".

var contentType = httpContext.Request.ContentType;

if (contentType != null && contentType != allowedContentType)
{
// ^^^^^^^^^^^^^
}

The warning is incorrect, because when I remove the null check, lots of my integration tests start to fail. Specifically, the ones where no Content-Type header is sent in the request.

This makes me believe the HttpRequest.ContentType property type should be string? instead of string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsbugThis issue describes a behavior which is not expected - a bug.feature-http-abstractionsgood first issueGood for newcomers.help wantedUp for grabs. We would accept a PR to help resolve this issuenullable

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions