Skip to content

Make it easier to return Problem Details responses for server errors #8537

Closed
@DamianEdwards

Description

@DamianEdwards

Add a set of new Problem helper methods on ControllerBase, that populates a ProblemDetails type with relevant values for simple returning of errors through the MVC response pipeline, e.g.:

private ObjectResult Problem()
{
    return Problem(detail: null);
}
 
private ObjectResult Problem(string detail)
{
    var problemDetails = new ProblemDetails
    {
        Title = "An error occurred while processing your request.",
        Detail = detail,
        Status = 500
    };
 
    problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? HttpContext.TraceIdentifier);
    return Problem(problemDetails);
}
 
private ObjectResult Problem(ProblemDetails problemDetails)
{
    return new ObjectResult(problemDetails) { StatusCode = problemDetails.Status };
}

@rynowak

Metadata

Metadata

Assignees

Labels

DoneThis issue has been fixedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing one

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions