Skip to content

Support more ProblemDetails Titles out-of-the-box for Results.Problem() #36417

Closed
@martincostello

Description

@martincostello

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:

  1. Make Microsoft.AspNetCore.Http.Results reference Microsoft.AspNetCore.WebUtilities, and then use ReasonPhrases.GetReasonPhrase(string) as a fallback.
  2. Refactor the HTTP status code descriptions into shared source, and then have both assemblies use it to enhance ObjectResult and/or ProblemDetailsDefaults and to provide the implementation for ReasonPhrases.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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority:3Work that is nice to havearea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etccost: SWill take up to 2 days to completefeature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labelstriage-focusAdd this label to flag the issue for focus at triage

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions