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) {} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
You can inherit from |
This comment has been minimized.
This comment has been minimized.
Just for understanding, why it was not included in the Asp.Net core? It's so obvious implementation. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
What if I want to return the same view for which the request has been made and pass the modelstate errors along. ?? |
This comment has been minimized.
This comment has been minimized.
@waqaskhan540 I have same question as you, did you solved? |
This comment has been minimized.
This comment has been minimized.
@lousaibiao try this:
Alternatively, you can invoke the action directly:
|
This comment has been minimized.
This comment has been minimized.
Instead of creating your own ModelStateValidationFilter one could use the build in ModelStateInvalidFilter. using Microsoft.AspNetCore.Mvc;
[assembly: ApiController] |
This comment has been minimized.
I hereby place this to public domain.