File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
src/WebApiContrib.Core.Formatter.Csv Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ using Microsoft . Extensions . DependencyInjection ;
2
+ using Microsoft . Net . Http . Headers ;
3
+ using System ;
4
+
5
+ namespace WebApiContrib . Core . Formatter . Csv
6
+ {
7
+ public static class CsvFormatterMvcCoreBuilderExtensions
8
+ {
9
+ public static IMvcCoreBuilder AddCsvSerializerFormatters ( this IMvcCoreBuilder builder )
10
+ {
11
+ if ( builder == null )
12
+ {
13
+ throw new ArgumentNullException ( nameof ( builder ) ) ;
14
+ }
15
+
16
+ return AddCsvSerializerFormatters ( builder , csvFormatterOptions : null ) ;
17
+ }
18
+
19
+ public static IMvcCoreBuilder AddCsvSerializerFormatters ( this IMvcCoreBuilder builder , CsvFormatterOptions csvFormatterOptions )
20
+ {
21
+ if ( builder == null )
22
+ {
23
+ throw new ArgumentNullException ( nameof ( builder ) ) ;
24
+ }
25
+
26
+ builder . AddFormatterMappings ( m => m . SetMediaTypeMappingForFormat ( "csv" , new MediaTypeHeaderValue ( "text/csv" ) ) ) ;
27
+
28
+ if ( csvFormatterOptions == null )
29
+ {
30
+ csvFormatterOptions = new CsvFormatterOptions ( ) ;
31
+ }
32
+
33
+ if ( string . IsNullOrWhiteSpace ( csvFormatterOptions . CsvDelimiter ) )
34
+ {
35
+ throw new ArgumentException ( "CsvDelimiter cannot be empty" ) ;
36
+ }
37
+
38
+ builder . AddMvcOptions ( options => options . InputFormatters . Add ( new CsvInputFormatter ( csvFormatterOptions ) ) ) ;
39
+ builder . AddMvcOptions ( options => options . OutputFormatters . Add ( new CsvOutputFormatter ( csvFormatterOptions ) ) ) ;
40
+
41
+ return builder ;
42
+ }
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments