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 commented Apr 21, 2016

I hereby place this to public domain.

@dotnetchris

This comment has been minimized.

Copy link

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 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 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 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 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 commented Aug 29, 2018

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

@smshellard

This comment has been minimized.

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);
      }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment