Created
April 21, 2016 06:19
-
-
Save Ciantic/88a2e9bae6961c23c2f366e89526b853 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) {} | |
} | |
} |
@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]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if I want to return the same view for which the request has been made and pass the modelstate errors along. ??