Support setting HTTP response headers using some Results methods for Minimal APIs #39585
Open
1 task done
Labels
area-minimal
Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Milestone
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
Using Minimal APIs with the built-in
Results
class' methods allows for lots of built-in functionality to perform lots of operations a developer may wish to achieve with an HTTP endpoint, but the experience isn't as good as soon as you need to set an HTTP response header.In this scenario you're left with at least three possible choices:
Results
with theHttpResponse
so you can manually set any headers in the endpoint before returning theIResult
.IResultExtensions
and a customIResult
implementation to write the body and any headers, likely duplicating implementation details from the internalIResult
implementations.Results
class and write to the content and headers to theHttpResponse
directly in the endpoint.As an example, take an endpoint which wishes to return an HTTP 429 problem if it wishes to rate limit the client and return a
Retry-After
response header. TheResults.Problem()
can be used to return the body and set the status code, but there is no way to set theRetry-After
header directly.To achieve the desired result, you need to either mix-and-match the lower-level
HttpResponse
usage in the endpoint (and potentially add an extra parameter to access it) with theResults
class, write a custom extension, or not useResults
at all.The code would be simpler for the developer in non-advanced cases if they could pass through a simple key-value pair of strings through the
Results
methods to set HTTP response headers without having to add additional complexity and/or concepts, reducing the "minimalness".Describe the solution you'd like
Some possible solutions to this (assuming it's not do nothing as this is considered an advanced use case) could include:
Results.Json()
andResults.Problem()
that accepts additional headers to set as anIDictionary<string, string>
, which would then be passed through to the internals ofObjectResult
for use in theConfigureResponseHeaders(HttpContext)
method.IResult
implementations public so behaviours could be easily overridden to extend them. For the HTTP 429 example, a developer could sub-classObjectResult.ConfigureResponseHeaders(HttpContext)
to set the additional header(s) (see also Make IResult methods more testable #37502)Additional context
No response
The text was updated successfully, but these errors were encountered: