Repository navigation
Conversation
HasJsonContentType runs for every minimal API JSON body (once in the RequestDelegateFactory check and again in ReadFromJsonAsync) and allocated a MediaTypeHeaderValue each time. Match "application/json" and "application/json; charset=utf-8" ordinal-ignore-case first; these parse to the same media type and charset, so results are unchanged. Everything else uses the existing parser. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The benchmark rewinds the stream but leaves the cached BodyReader exhausted, causing subsequent invocations to throw JsonException instead of measuring successful deserialization.
1 open issue
What changed in this PR
This PR reduces parsing allocations in HTTP request JSON handling by fast-pathing two common content types while retaining the existing parser for other values.
Changes:
- Adds case-insensitive checks for
application/jsonandapplication/json; charset=utf-8. - Extends tests for casing, spacing, charset, and fallback behavior.
- Adds benchmarks for content-type checks and JSON deserialization.
| File | Description |
|---|---|
src/Http/Http/perf/Microbenchmarks/HttpRequestJsonContentTypeBenchmarks.cs |
Adds content-type and deserialization benchmarks. |
src/Http/Http.Extensions/test/HttpRequestJsonExtensionsTests.cs |
Expands content-type recognition and UTF-8 deserialization coverage. |
src/Http/Http.Extensions/src/HttpRequestJsonExtensions.cs |
Bypasses media-type parsing for two common JSON content types. |
Review effort: Balanced
Co-authored-by: Copilot <[email protected]>
|
Heads-up for reviewers: this PR overlaps with #70119, which was opened earlier from the same account and adds an allocation-free fast path to the same private 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI with an empty commit (556eb2f). The previous run failed only the Blazor E2E test 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
CI update:
That pipeline is failing on 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |

HttpRequestJsonExtensions.HasJsonContentTypecallsMediaTypeHeaderValue.TryParseon every call. That allocates aMediaTypeHeaderValue, plus a parameter list andNameValueHeaderValuewhen a charset is present. For a minimal API endpoint with a JSON body it runs twice per request: once in theRequestDelegateFactory415 check and again insideReadFromJsonAsync.This change checks for the two overwhelmingly common values first, using an ordinal-ignore-case compare:
application/jsonapplication/json; charset=utf-8(whatHttpClient'sJsonContentsends)These exact strings always parse to the JSON media type, with no charset in the first case and
utf-8in the second. The fast path returns the same result and an equivalent charset (GetEncodingFromCharsetcompares case-insensitively). Every other value, including+jsonsuffixes, other charsets, extra parameters and odd whitespace, goes through the existing parser unchanged.Benchmark
BenchmarkDotNet InProcess,
HttpRequestJsonContentTypeBenchmarks(added). Three interleaved rounds:application/jsonapplication/json; charset=utf-8application/problem+json(not fast-pathed)DefaultHttpContext+ small object, charset=utf-8)Content types that miss the fast path pay about 10 ns for the two failed compares.
Tests
Added cases to the
HasJsonContentTypetheory: no-space charset, utf-16, extra parameters,application/jsonxand an unknown charset. Added aReadFromJsonAsynctheory over casing and spacing variants of the fast-path values. All of these pass on both main and this branch. All 2304 Http.Extensions tests pass.