A response header is set with Headers.Add, which throws when the header is already present

tx0/csharp:header-added-twice-throwsstrictwarningreliability/error-handlingdepth fastbug

What it finds

The header dictionary of ASP.NET Core throws ArgumentException from Add when the header exists, which happens when middleware or another branch set it first. The indexer, Append, or the typed properties such as Headers.CacheControl set or extend the value instead. The rule reports Headers.Add in files that use ASP.NET Core.

From the rule's source, header_added_twice_throws.rs.

Example from the rule's tests
using Microsoft.AspNetCore.Http; class Caching{    void Set(HttpContext context)    {        context.Response.Headers.Add("Cache-Control", "no-store");tx0 reports: Headers.Add throws if the header is already set        context.Response.Headers["Pragma"] = "no-cache";    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:header-added-twice-throws" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:header-added-twice-throws] reason="..."

tx0 explain tx0/csharp:header-added-twice-throws shows the same in the terminal. Found a false positive? Report it.