Skip to content

Allow ProblemDetails property to be settable in ProblemDetailsContext #47633

@bjornen77

Description

@bjornen77

Background and Motivation

Currently the ProblemDetail property in ProblemDetailsContext is init only, which makes it impossible to change the ProblemDetails instance to another type if wanted when customizing the problem details.

For example, when sending invalid JSON to an API controller a validation problem is returned. If I would like to change this to another problem details type, the property needs to be settable.

Proposed API

namespace Microsoft.AspNetCore.Http;

public class ProblemDetailsContext
{
-   public ProblemDetails ProblemDetails { get; init; }
+   public ProblemDetails ProblemDetails { get; set; }
}

Usage Examples

builder.Services.AddProblemDetails(options =>
{
    options.CustomizeProblemDetails = (context) => 
    {
        if (context.ProblemDetails is HttpValidationProblemDetails)
        {
            // change to another problem details type because the HttpValidationProblemDetails contains
            // an Errors property that might not be wanted in some cases.
            context.ProblemDetails = new ProblemDetails() { .... }
        }
    };
});

Alternative Designs

N/A

Risks

Investigate so it has no side effect of substituting the problem details instance here,

Metadata

Metadata

Assignees

Labels

Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions