Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created April 21, 2016 06:19
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Ciantic/88a2e9bae6961c23c2f366e89526b853 to your computer and use it in GitHub Desktop.
Save Ciantic/88a2e9bae6961c23c2f366e89526b853 to your computer and use it in GitHub Desktop.
Model state validation filter ASP.NET Core
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) {}
}
}
@IeuanWalker
Copy link

public class ModelStateValidationFilter : ActionFilterAttribute
{
     public override void OnActionExecuting(ActionExecutingContext context)
     {
         if (!context.ModelState.IsValid)
         {
             List<string> list = (from modelState in context.ModelState.Values from error in modelState.Errors select error.ErrorMessage).ToList();
             context.Result = new BadRequestObjectResult(list);
         }

         base.OnActionExecuting(context);
     }
}

@gregoryagu
Copy link

gregoryagu commented Mar 14, 2018

public class ModelStateValidationFilter : ActionFilterAttribute
  {
    public override void OnActionExecuting(ActionExecutingContext context)
    {
      if (!context.ModelState.IsValid)
      {
        List<string> list = (from modelState in context.ModelState.Values from error in modelState.Errors select error.ErrorMessage).ToList();

        //Also add exceptions.
        list.AddRange(from modelState in context.ModelState.Values from error in modelState.Errors select error.Exception.ToString());

        context.Result = new BadRequestObjectResult(list);
      }

      base.OnActionExecuting(context);
    }
  }

@waqaskhan540
Copy link

What if I want to return the same view for which the request has been made and pass the modelstate errors along. ??

@lousaibiao
Copy link

@waqaskhan540 I have same question as you, did you solved?

@smshellard
Copy link

smshellard commented Sep 6, 2018

@lousaibiao try this:

public override void OnActionExecuting(ActionExecutingContext context)
{
   if (!context.ModelState.IsValid)
   {
      var controller = context.Controller as Controller;
      var model = context.ActionArguments?.Count > 0
         ? context.ActionArguments.First().Value
         : null;
      context.Result = (IActionResult)controller?.View(model)
         ?? new BadRequestResult();
   }
   
   base.OnActionExecuting(context);
}

Alternatively, you can invoke the action directly:

      public override void OnActionExecuting(ActionExecutingContext context)
      {
         if (!context.ModelState.IsValid)
         {
            var action = context.ActionDescriptor as ControllerActionDescriptor;
            var controller = context.Controller as Controller;
            context.Result = (IActionResult)action?.ControllerTypeInfo.InvokeMember(
               action.ActionName,
               BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
               null,
               controller,
               null)
               ?? new BadRequestResult();
         }

         base.OnActionExecuting(context);
      }

@TimHolzherr
Copy link

Instead of creating your own ModelStateValidationFilter one could use the build in ModelStateInvalidFilter.
This filter is automatically registered on your controller if you decorate it with the ApiController attribute. The ApiController Attribute will enable additional api specific behaviors which are documented in the link above. To apply the ApiController attribute to all controllers in your assembly one can decorate the Assembly itself with the attribute. Just add the following code to a *.cs file which is part of the assembly.

using Microsoft.AspNetCore.Mvc;

[assembly: ApiController]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment