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

ASP.NET Core – How to unit test a custom InputFormatter

In this article, I’ll show how to unit test a custom InputFormatter. The main thing to test is the output of the ReadRequestBodyAsync() method. To test this, you have to pass in an InputFormatterContext object containing the request body. As an example, I’ll show how to unit test the following ReadRequestBodyAync() method: Note: This parses … Read more

ASP.NET Core – Add a custom InputFormatter

Input formatters are used to deserialize the request body to a model object (which is then passed into an action method). There are built-in input formatters for handling JSON and XML. You can add your own input formatter when you want to customize request body deserialization. There are two scenarios where a custom InputFormatter would … Read more

ASP.NET Core – How to receive a request with text/plain content

When a request comes in and your action method has parameters, the framework tries to find the appropriate InputFormatter to handle deserializing the request data. There’s no built-in text/plain InputFormatter though, so when you send a request with text/plain content, it fails with a 415 – Unsupported Media Type error response. In this article, I’ll … Read more

C# – How to send synchronous requests with HttpClient

In .NET 5 and above, you can use the HttpClient Sync API methods – Send() and ReadAsStream() – to send HTTP requests synchronously (as opposed to resorting to sync-over-async). Here’s the steps for doing this: HttpClient was originally designed for async requests and has many async convenience methods (like GetAsync() and ReadAsStringAsync()). There aren’t sync … Read more

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

When you’re parsing CSV with CsvHelper and there’s no header row, you have to configure it to map by field position. I’ll show how to do that. 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 … 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, read the file line by line and split each line with a comma. This gives you a string array containing the fields that you can … Read more

C# – Use yield return to minimize memory usage

Let’s say you want to search for specific characters in a large text file and return a list of context objects to the calling code for further processing (such as showing the results in the UI). One way to do that is to build the entire list at once and return it. If you don’t … Read more

C# – Save a list of strings to a file

The simplest way to save a list of strings to a file is to use File.WriteAllLines(). This creates (or overwrites) the file and writes each string on a new line. The resulting file looks like this: Note: Showing non-printable newline characters \r\n for clarity. Specifying the separator character What if you want to separate each … Read more

C# – Get types from assembly (reflection-only load)

You can get all types from an assembly by doing a reflection-only load. This allows you to read type info from metadata without running the typical errors associated with fully loading an assembly. The way to do a reflection-only load is different in .NET Framework and .NET Core. I’ll show examples of how to do … Read more

ASP.NET Core – How to unit test your middleware class

There are three requirements for unit testing a middleware class: Here’s an example: This is a simple test that only checks the response status code. By passing in DefaultHttpContext, you have control over the request and response objects. You can set the request to whatever you need, and then verify the response. I’ll show examples … Read more

C# – How to consume an SSE endpoint with HttpClient

Server-Sent Events (SSE) allow a client to subscribe to messages from the server. It creates a one-way stream from the server to the client. When the server has new messages for the client, it writes them to the stream. This is an alternative to the client polling the server for updates. Use the following to … 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