File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Mvc . Filters ;
2+ using Microsoft . AspNetCore . Mvc . Formatters ;
3+ using Microsoft . AspNetCore . Mvc ;
4+ using Newtonsoft . Json . Serialization ;
5+ using Newtonsoft . Json ;
6+ using System . Buffers ;
7+
8+ namespace Web . Attributes
9+ {
10+ public class JsonConfigFilterAttribute : ActionFilterAttribute
11+ {
12+ public override void OnResultExecuting ( ResultExecutingContext context )
13+ {
14+ if ( context . Result is ObjectResult objectResult )
15+ {
16+ var serializerSettings = new JsonSerializerSettings
17+ {
18+ NullValueHandling = NullValueHandling . Include ,
19+ DateTimeZoneHandling = DateTimeZoneHandling . Unspecified ,
20+ ContractResolver = new CamelCasePropertyNamesContractResolver ( )
21+ } ;
22+
23+ var jsonOutputFormatter = new NewtonsoftJsonOutputFormatter (
24+ serializerSettings ,
25+ ArrayPool < char > . Shared ,
26+ new MvcOptions { } ,
27+ new MvcNewtonsoftJsonOptions ( )
28+ ) ;
29+
30+ objectResult . Formatters . Add ( jsonOutputFormatter ) ;
31+ }
32+
33+ base . OnResultExecuting ( context ) ;
34+ }
35+ }
36+ }
Original file line number Diff line number Diff line change @@ -5,8 +5,10 @@ namespace Web.Controllers
55 using Microsoft . AspNetCore . Mvc ;
66 using System . Net . Mail ;
77 using System . Net ;
8+ using Web . Attributes ;
89
910 [ Route ( "api/reports" ) ]
11+ [ JsonConfigFilter ]
1012 public class ReportsController : ReportsControllerBase
1113 {
1214 public ReportsController ( IReportServiceConfiguration reportServiceConfiguration )
You can’t perform that action at this time.
0 commit comments