Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created November 3, 2021 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/e2fa268ed1cbc2b3c788800e51655f4e to your computer and use it in GitHub Desktop.
Save dcomartin/e2fa268ed1cbc2b3c788800e51655f4e to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class DemoController : ControllerBase
{
[HttpPost]
public ActionResult Post()
{
var problemDetails = new ProblemDetails
{
Detail = "The request parameters failed to validate.",
Instance = null,
Status = 400,
Title = "Validation Error",
Type = "https://example.net/validation-error",
};
problemDetails.Extensions.Add("invalidParams", new List<ValidationProblemDetailsParam>()
{
new("name", "Cannot be blank."),
new("age", "Must be great or equals to 18.")
});
return new ObjectResult(problemDetails)
{
StatusCode = 400
};
}
}
public class ValidationProblemDetailsParam
{
public ValidationProblemDetailsParam(string name, string reason)
{
Name = name;
Reason = reason;
}
public string Name { get; set; }
public string Reason { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment