Read and write to a CSV file example in ASP.NET Core

You can read from and write to a CSV file when you download this code example. It will go through a number of examples of how to read and write to a CSV file in an ASP.NET Core Web API.

You can find out more about reading from and writing to a CSV file, or watch the video.

Download the code example

You'll need to fill out the code example form. We will send you an email where you can download the code example.

The code example will be in a zip file.

Software

This is the software that will need to be installed onto your machine.

Run the application

Open up RoundTheCode.CsvReadWrite.sln in Visual Studio and run the application. It should load up the Scalar documentation at https://localhost:4902/scalar/v1. Here is what you can run:

  • https://localhost:4902/api/csv-read - Reads Files/Products.csv from the Api project and returns it as a JSON response
  • https://localhost:4902/api/csv-read/no-header - Reads Files/ProductsNoHeader.csv from the Api project and returns it as a JSON response
  • https://localhost:4902/api/csv-read/product-name - Reads only the product name in Files/Products.csv from the Api project and returns it as a JSON response
  • https://localhost:4902/api/csv-read/different-header-names - Reads Files/ProductsDifferentHeaderNames.csv from the Api project and returns it as a JSON response
  • https://localhost:4902/api/csv-read/by-line - Reads Files/Products.csv by line from the Api project and returns it as a JSON response
  • https://localhost:4902/api/csv-read/products-csv-file-contents - Reads all the contents from Files/Products.csv and outputs it to the response
  • https://localhost:4902/api/csv-write POST - Adds category data to Files/Category-{UtcNow}.csv (Where {UtcNow} is the UTC time now in yyyyMMdd-HHmmss format)