-
-
Save Ciantic/88a2e9bae6961c23c2f366e89526b853 to your computer and use it in GitHub Desktop.
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) {} | |
} | |
} |
You can inherit from ActionFilterAttribute
, override OnActionExecuting
, and then you can delete the empty method of OnActionExecuted
Just for understanding, why it was not included in the Asp.Net core? It's so obvious implementation.
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);
}
}
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);
}
}
What if I want to return the same view for which the request has been made and pass the modelstate errors along. ??
@waqaskhan540 I have same question as you, did you solved?
@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);
}
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]
I hereby place this to public domain.