Adds CSV formatted response support in ASP.NET Web API
Register the CsvFormatter
in Register
(in WebApiConfig.cs):
config.Formatters.Add(new CsvFormatter());
or to specify a Func
config.Formatters.Add(new CsvFormatter { Selector = func } );
Now if an Accept
header with a value of text/csv
is supplied the response will be automatically formatted as CSV before being returned
CsvReponse accepts a Func
to control which object is used as the source when a complex object is used, see examples below.
You can now control the column headers by using the new CsvColumn
attribute, see examples below.
Well it's a comma (,) as in Comma Separated Values :-)
If these characters are encountered they will be wrapped within double quotes ("")
The fields
parameter controls which fields are returned in the response.
The following rules explain the supported syntax for the fields
parameter value:
fields=id,name
to select multiple fieldsfields=*
to select all fields
If fields
is omitted all fields are returned
Given an object like this
class Product {
public int Id {get; set;}
public string Name {get; set;}
public string Barcode {get; set;}
public decimal Cost {get; set;}
}
Will give the following output...
https://myapi.mycompany.com/products
"Id", "Name", "Barcode", "Cost"
1,Banana,08765412,0.45
2,Apple,256895,0.75
3,Orange,895698,0.60
https://myapi.mycompany.com/products?fields=id,name
"Id", "Name"
1,Banana
2,Apple
3,Orange
https://myapi.mycompany.com/products?fields=id,name,cost
"Id", "Name", "Cost"
1,Banana,0.45
2,Apple,0.75
3,Orange,0.60
Given an object like this
class Customer {
public int Id {get; set;}
public string Name {get; set;}
public List<Address> Addresses {get; set;}
}
class Address {
public string Street {get; set;}
public string Town {get; set;}
public string County {get; set;}
public string Postcode {get; set;}
}
and a Func
like this
Func<object, HttpRequestMessage, object> func = (o, h) => ((Customer)o).Addresses;
https://myapi.mycompany.com/customers
"Street", "Town", "County", "Postcode"
Davigdor Road,Hove,East Sussex,BN31RE
If we modify the object to include the CsvColumn
attribute
class Product {
[CsvColumn(Name="Product Id")]
public int Id {get; set;}
[CsvColumn(Name="Product Name")]
public string Name {get; set;}
public string Barcode {get; set;}
[CsvColumn(Name="RRP")]
public decimal Cost {get; set;}
}
Will give the following output...
https://myapi.mycompany.com/products
"Product Id", "Product Name", "Barcode", "RRP"
1,Banana,08765412,0.45
2,Apple,256895,0.75
3,Orange,895698,0.60