Description
Is your feature request related to a problem? Please describe.
If a developer using Minimal Actions uses the Results.Problem()
method with an HTTP status code that is not part of ProblemDetailsDefaults
by default, then without it being explicitly set themselves, a default title
is not returned in the response.
Take the following intentionally obscure example:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/teapot", () => Results.Problem("I like tea", statusCode: 418));
app.Run();
Calling the /teapot
resource will return the following JSON:
{"status":418,"detail":"I like tea"}
If more default HTTP status codes were built in, the default experience would be more informative:
{"title":"I'm a teapot","status":418,"detail":"I like tea"}
Describe the solution you'd like
A more comprehensive list of HTTP status code descriptions are available in the ReasonPhrases
class.
However, using it directly in ObjectResult
here is not possible as Microsoft.AspNetCore.WebUtilities
is not referenced by Microsoft.AspNetCore.Http.Results
.
The out-of-the-box experience for Results.Problem()
could be improved with one of the following:
- Make
Microsoft.AspNetCore.Http.Results
referenceMicrosoft.AspNetCore.WebUtilities
, and then useReasonPhrases.GetReasonPhrase(string)
as a fallback. - Refactor the HTTP status code descriptions into shared source, and then have both assemblies use it to enhance
ObjectResult
and/orProblemDetailsDefaults
and to provide the implementation forReasonPhrases.GetReasonPhrase(string)
.
Otherwise, the developer has to manually specify the title for any HTTP status codes not already catered for, like this:
// using Microsoft.AspNetCore.WebUtilities;
app.MapGet("/teapot", () => Results.Problem("I like tea", statusCode: 418, title: ReasonPhrases.GetReasonPhrase(418)));
Additional context
Found using .NET SDK version 6.0.100-rc.1.21460.8