ASP.NET Core – Receive a request with CSV data

There are two ways to receive CSV data in a web API: In this article, I’ll show examples of both of these approaches. I’ll be using the CsvHelper library to parse CSV data into model objects and then do model validation. Note: To use CsvHelper, install the CsvHelper package (Install-Package CsvHelper). Or use whichever parser … Read more

CsvHelper – Header with name not found

When your CSV header names don’t match your property names exactly, CsvHelper will throw an exception. For example, if your header name is “title” and your property name is “Title”, it’ll throw an exception like: HeaderValidationException: Header with name ‘Title'[0] was not found. If you don’t want to (or can’t) change the names to match, … Read more

C# – Using CsvHelper when there’s no header row

In this article, I’ll show how to configure CsvHelper to map CSV fields to the right properties when there’s no header row. At the end, I’ll show the alternative approach of manually parsing in this scenario. Consider the following CSV data without a header row: Normally, CsvHelper maps fields to properties by matching column names … Read more

C# – Parsing a CSV file

In this article, I’ll show how to parse a CSV file manually and with a parser library (CsvHelper). Let’s say you have the following CSV file: To manually parse this, split each line with a comma and process the fields in the resulting string[] however you want. If there’s a header row, skip it (headers … Read more

C# – Parsing CSV data when a field has commas

When you have commas in your CSV fields, it creates a conflict with the field delimiting commas. In other words, you can’t tell which data belongs to which field. How you deal with this will depend on one question: is the field with the comma enclosed in quotes? Comma is enclosed in quotes Spreadsheet programs … Read more