Q:

.NET Core Web API: return a custom HTTP status code with Content Type application/problem+json

ControllerBase contains methods such as Conflict() that return a ConflictResult object (representing an HTTP 409 response) that is derived from StatusCodeResult.  The resulting response body has content type application/problem+json and looks like this:
{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.8",
    "title": "Conflict",
    "status": 409,
    "traceId": "0HLO99QHFC9QI:00000001"
}

There is no in-built method/class for an HTTP 410 response, so I made one:
[DefaultStatusCode(410)]
public class GoneResult : StatusCodeResult
{
    public GoneResult() : base(410)
    {}
}
...
public static class ControllerBaseExtensions
{
    public static GoneResult Gone(this ControllerBase controllerBase) // this doesn't give all the problem+JSON attributes
    {
        return new GoneResult();
    }
}

However, this gives 
{
    "type": "about:blank",
    "status": 410
}

i.e., the type value is different and the title and traceId fields are missing.
I'd also like to create a custom class for an HTTP 500 response that includes a message field with the error message.  I've tried returning StatusCode(StatusCodes.Status500InternalServerError), which gives me the same minimal application/problem+json response as my Gone() method; I've also tried returning StatusCode(StatusCodes.Status500InternalServerError, message), which gives me my error message but formats the response as text/plain.

A:

The code that generates the ProblemDetails response isn't aware of the 410 status-code, so it doesn't have an associated Link and Title property to use when building the response object. To add this awareness, configure ApiBehaviorOptions in ConfigureServices, like this:
services.Configure<ApiBehaviorOptions>(options =>
{
    options.ClientErrorMapping[410] = new ClientErrorData
    {
        Title = "Gone",
        Link = "https://tools.ietf.org/html/rfc7231#section-6.5.9"
    };
});

ClientErrorMapping is a dictionary of int (status-code) to ClientErrorData. Note that the value I've used for Link above does point to the correct section of the RFC.

