- Home
- .NET code examples
- Read and write to a CSV file example in ASP.NET Core
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.
- Visual Studio 2022. Version 17.12.0 or above. It will work with the free community version.
- .NET 9 SDK. Version 9.0.0 or above.
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- ReadsFiles/Products.csvfrom theApiproject and returns it as a JSON responsehttps://localhost:4902/api/csv-read/no-header- ReadsFiles/ProductsNoHeader.csvfrom theApiproject and returns it as a JSON responsehttps://localhost:4902/api/csv-read/product-name- Reads only the product name inFiles/Products.csvfrom theApiproject and returns it as a JSON responsehttps://localhost:4902/api/csv-read/different-header-names- ReadsFiles/ProductsDifferentHeaderNames.csvfrom theApiproject and returns it as a JSON responsehttps://localhost:4902/api/csv-read/by-line- ReadsFiles/Products.csvby line from theApiproject and returns it as a JSON responsehttps://localhost:4902/api/csv-read/products-csv-file-contents- Reads all the contents fromFiles/Products.csvand outputs it to the responsehttps://localhost:4902/api/csv-writePOST- Adds category data toFiles/Category-{UtcNow}.csv(Where{UtcNow}is the UTC time now inyyyyMMdd-HHmmssformat)
Give us your anonymous feedback regarding this page, or any other areas of the website.
Watch .NET tutorials
Subscribe to our YouTube channel
Latest code examples
Request logging in ASP.NET Core example to save logs to a DB
Download a request logging in ASP.NET Core example that uses middleware to save logs to the database with Entity Framework Core and SQL Server.