Custom Middleware in ASP.NET

Option #1: Implementing IMiddleware Add the middleware to DI, and then use it. In the middleware class, use DI for the constructor, and implement the single InvokeAsync method. Option #2: By Convention Use the middleware, but you don’t need to register it in DI. In the middleware class constructor, accept and store the RequestDelegate, then […]

.NET Console Application Template

Here’s what I’ll use as the starting point for any new console application that I build. It includes simple logging to the console and to a file, dependency injection, and strongly typed configuration, including user secrets that don’t go to source control. Main.cs Program.cs appsettings.json secrets.json (in your user directory, not in the project directory) […]

Appending a binary file in C#

To add bytes to the end of a binary file, this is the basic code needed: If you’re potentially bringing in a lot of data, make sure you use a stream for the incoming data and write a little at a time. For a small amount of data, you can use a ReadOnlySpan<byte> instead of […]

Appending a text file in C#

Here are the basic ways of appending text to an existing file. Each of these will create a new file if the file does not already exist. Each are useful in different situations. All three of these have an equivalent Async version as well.

Writing to text file in C#

Here are the basic ways of writing text to a file. Each of these will create a new file, or overwrite if the file already exists. Each are useful in different situations. All three of these have an equivalent Async version as well.

TypeScript settings in csproj

These are the settings I put in the csproj file for TypeScript – it sets values to convert to ES5, takes all TypeScript files and compiles to individual JavaScript files in the /Scripts/js directory. From there, I’ll bundle them into a single site.js file inside of wwwroot/js using BunderMinifier.