-
-
Save ansarizafar/4c08c52a72cf68af6db64c0d5c379198 to your computer and use it in GitHub Desktop.
Model state validation filter ASP.NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.AspNetCore.Mvc.Filters; | |
namespace Example | |
{ | |
public class ModelStateValidationFilter : Attribute, IActionFilter | |
{ | |
public void OnActionExecuting(ActionExecutingContext context) | |
{ | |
if (!context.ModelState.IsValid) { | |
context.Result = new BadRequestObjectResult(context.ModelState); | |
} | |
} | |
public void OnActionExecuted(ActionExecutedContext context) {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment