Files
myoffice/MyOffice.Web/Infrastructure/Attributes/GlobalModelStateValidatorAttribute.cs
2023-12-16 21:18:01 +02:00

29 lines
808 B
C#

namespace MyOffice.Web.Infrastructure.Attributes;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
public class GlobalModelStateValidatorAttribute : ActionFilterAttribute
{
private readonly ProblemDetailsFactory _problemDetailsFactory;
public GlobalModelStateValidatorAttribute(ProblemDetailsFactory problemDetailsFactory)
{
_problemDetailsFactory = problemDetailsFactory;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
context.Result = new ObjectResult(_problemDetailsFactory.CreateValidationProblemDetails(context.HttpContext, context.ModelState))
{
StatusCode = StatusCodes.Status400BadRequest
};
}
base.OnActionExecuting(context);
}
}