File tree Expand file tree Collapse file tree
ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Mvc ;
2+ using Microsoft . AspNetCore . Mvc . Filters ;
3+
4+ namespace ReferenceProject . Filters
5+ {
6+ public class ValidateModelFilter : IActionFilter
7+ {
8+ public void OnActionExecuted ( ActionExecutedContext context )
9+ {
10+
11+ }
12+
13+ public void OnActionExecuting ( ActionExecutingContext context )
14+ {
15+ if ( ! context . ModelState . IsValid )
16+ {
17+ context . Result = new BadRequestObjectResult ( context . ModelState ) ;
18+ }
19+ }
20+ }
21+ }
Original file line number Diff line number Diff line change 1313using Microsoft . Extensions . Logging ;
1414using Newtonsoft . Json ;
1515using Newtonsoft . Json . Serialization ;
16+ using ReferenceProject . Filters ;
1617using ReferenceProject . Middleware ;
1718using ReferenceProject . Modules ;
1819
@@ -62,7 +63,10 @@ public void ConfigureServices(IServiceCollection services)
6263 . AddScoped < IUrlHelper > ( x => x
6364 . GetRequiredService < IUrlHelperFactory > ( )
6465 . GetUrlHelper ( x . GetRequiredService < IActionContextAccessor > ( ) . ActionContext ) )
65- . AddMvc ( )
66+ . AddMvc ( options =>
67+ {
68+ options . Filters . Add ( new ValidateModelFilter ( ) ) ;
69+ } )
6670 . AddJsonOptions ( options =>
6771 {
6872 options . SerializerSettings . NullValueHandling = NullValueHandling . Ignore ;
You can’t perform that action at this time.
0 commit comments