1

ASP.NET Core 3.1 is taking POSTed JSON (for creating a user via SCIM v2 in my fledgling Web API) and model-binding it to my C# request-data class CreateUserRequest like a champ. And it produces nice ProblemDetails-based errors if there is an error in the JSON.

E.g. if the user's last name (aka family name) is missing (which it shouldn't be because I [Require] that property) then the response JSON is:-

{
  "type": "https://www.rfc-editor.org/rfc/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "|dbd3da85-4991c45298582047.",
  "errors": {
    "Name.LastName": [
      "The LastName field is required.",
      "The field LastName must be a string with a minimum length of 1 and a maximum length of 100."
    ]
  }
}

This is the result of ValidationProblemDetails and it's good for most purposes, but unfortunately it's not good for SCIM, which requires a different JSON format, described here.

E.g. SCIM errors look something like this:-

   {
     "schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"],
     "scimType":"mutability"
     "detail":"Attribute 'id' is readOnly",
     "status": "400"
   }

How to replace ProblemDetails and ValidationProblemDetails with something else (in this case something SCIM-compatible) in ASP.NET Core 3.1 Web API?

Community
  • 1
  • 1
Bellarmine Head
  • 3,397
  • 2
  • 22
  • 31
  • 1
    [How can I customize the error response in Web API with .NET Core?](https://stackoverflow.com/questions/54942192/how-can-i-customize-the-error-response-in-web-api-with-net-core) – Pavel Anikhouski Feb 18 '21 at 21:08
  • 1
    Thanks @PavelAnikhouski I was searching SO mainly for "problemdetails" etc. and so missed that link. I also found https://stackoverflow.com/questions/51145243/how-do-i-customize-asp-net-core-model-binding-errors – Bellarmine Head Feb 19 '21 at 18:26
  • Chris Pratt is the one who has discovered the magic formula: set your own `InvalidModelStateResponseFactory`. [Microsoft docs link](https://learn.microsoft.com/en-us/aspnet/core/web-api/handle-errors?view=aspnetcore-3.1#validation-failure-error-response) – Bellarmine Head Feb 19 '21 at 18:29

0 Answers0