Skip to content

ResultHeaderExtensions Class

Attaches response headers to a result instead of writing them beside it.

C#
public static class ResultHeaderExtensions

Inheritance System.Object → ResultHeaderExtensions

Methods

ResultHeaderExtensions.WithHeaders(this IResult, Action<IHeaderDictionary>) Method

Returns a result that sets the given headers and then renders result.

C#
public static Microsoft.AspNetCore.Http.IResult WithHeaders(this Microsoft.AspNetCore.Http.IResult result, System.Action<Microsoft.AspNetCore.Http.IHeaderDictionary> configure);

Parameters

result Microsoft.AspNetCore.Http.IResult

The answer being rendered.

configure System.Action<Microsoft.AspNetCore.Http.IHeaderDictionary>

Sets the headers on the response.

Returns

Microsoft.AspNetCore.Http.IResult

Remarks

A header a specification requires belongs to the answer, not to the code path that happened to produce it. Written as a statement before the return, it is attached to whichever branch the author was looking at and quietly missing from the others - and a header required on a refusal is required on the branch least often read. Carried by the result, it travels with every answer that result stands for.

The headers are set when the result is executed, which is before anything is written to the body: a header added after the response has started is dropped, and ASP.NET Core reports that as an exception rather than as the silent loss it would otherwise be.