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)
Download this code example
Enter your email and we'll send you a download link.
Latest code examples
Download a .NET 10 Web API integrated with Minimal APIs and SQL Server alongside Entity Framework Core and FluentValidation.
Download a C# code example to help you learn variables, types, conditionals, arrays, lists, loops, classes, structs, interfaces, enums and static classes.
Download this code example
Enter your email and we'll send you a download link.