Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created Nov 3, 2021
Embed
What would you like to do?
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