Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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) {}
}
}
@Ciantic

This comment has been minimized.

Copy link
Owner Author

@Ciantic Ciantic commented Apr 21, 2016

I hereby place this to public domain.

@dotnetchris

This comment has been minimized.

Copy link

@dotnetchris dotnetchris commented Jan 13, 2017

You can inherit from ActionFilterAttribute, override OnActionExecuting, and then you can delete the empty method of OnActionExecuted

@dmitriyse

This comment has been minimized.

Copy link

@dmitriyse dmitriyse commented Mar 9, 2017

Just for understanding, why it was not included in the Asp.Net core? It's so obvious implementation.

@IeuanWalker

This comment has been minimized.

Copy link

@IeuanWalker IeuanWalker commented Oct 5, 2017

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

This comment has been minimized.

Copy link

@gregoryagu 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

This comment has been minimized.

Copy link

@waqaskhan540 waqaskhan540 commented Mar 30, 2018

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

@lousaibiao

This comment has been minimized.

Copy link

@lousaibiao lousaibiao commented Aug 29, 2018

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

@smshellard

This comment has been minimized.

Copy link

@smshellard 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

This comment has been minimized.

Copy link

@TimHolzherr TimHolzherr commented Feb 24, 2021

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