aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

Unable to map error codes to custom problem details via `ApiBehaviorOptions` #8611

Closed mkArtakMSFT closed 5 years ago

mkArtakMSFT commented 5 years ago

In my controller (ApiController) the action returns BadRequest():

return BadRequest();

In the startup I have the following mapping defined:

services.Configure<ApiBehaviorOptions>(options =>
{
    options.ClientErrorMapping.Add(400, new ClientErrorData { Link = "sample link", Title = "fake error customized" });
});

But this has no effect and client gets the usual ProblemDetails error - no matter whether I have that mapping or not:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "Bad Request",
    "status": 400,
    "traceId": "0HLHJ0KNGN7K7:00000002"
}
pranavkm commented 5 years ago

Likely the same cause as https://github.com/aspnet/Mvc/issues/8564. The order of when Configure is called matters. Using https://github.com/aspnet/Mvc/pull/8618 as a way to make this less brittle.

pranavkm commented 5 years ago

Introduced a new API to allow configuring ApiBehaviorOptions. The guidance is to use IMvcBuilder.ConfigureApiBehaviorOptions to configure the app over services.Configure<ApiBehaviorOptions>