Senior Application Developer. MBA. I write blogs about .NET, Angular, JavaScript/TypeScript, Docker, AWS, DDD, and many others.

How to return a file from an API endpoint in ASP.NET Core and create an HTML download link.

Image for post
Image for post

In this article, I will use a demo Web API application in ASP.NET Core to show you how to transmit files through an API endpoint. In the final HTML page, end users can left-click a hyperlink to download the file or right-click the link to choose “” in the context menu and save the file.

The full solution can be found in my GitHub repository, which includes a web project for uploading/downloading files and an integration test project for testing controller methods.

Now let’s dive in.

Return a File in ASP.NET Core Web API

In ASP.NET Core, a Web API action method usually returns an


Surprise! The arrays are not replaced as a whole.

Image for post
Image for post

ASP.NET Core uses configuration providers to read configuration key-value pairs from a variety of sources. You can read more about configuration in ASP.NET Core in its official documentation, which has covered tons of use cases in detail.

Recently, when I set a JSON array in the files in an ASP.NET Core project, the result of the final configuration data surprised me. The array did not behave the same as other JSON objects that could be overridden in an environment-specific file. Instead, the final array could have more elements than expected. After some googling, I found that there are…


How to create an immutable collection of cache objects and how to unit test IMemoryCache

Image for post
Image for post

Caching is a common technique to reduce the number of trips for fetching data that seldom changes. Thus we can take advantage of caching to boost our application’s performance. ASP.NET Core provides several caching libraries, including in-memory caching, distributed caching, and many others. Among these caching techniques, , the simplest cache, is included in an ASP.NET Core web project by default and works natively with dependency injection.

There are many introductory articles talking about using the to store data in the memory of the webserver. However, few of them have mentioned how to ensure the consistency of cached values…


How to upload multiple files through one HTTP request to an API endpoint

Image for post
Image for post

I wrote an article entitled Upload Files with Angular and .NET Web API recently. To continue on this subject, in this article I will show you the details of creating an Angular component to upload multiple files within one HTTP request to an API endpoint. The demo app works as the following screen recording shows.


How to write integration tests for API endpoints using MSTest

Image for post
Image for post

We often want to write integration tests to evaluate our app’s whole request-response pipeline. For example, we want our API endpoints to return the same responses given the same requests. These integration tests are helpful in catching cases of missing properties in the JSON responses, mismatching upper/lower cases of property names, obsolete error messages, and so on.

The ASP.NET official documentation has an article entitled Integration tests in ASP.NET Core, which covers lots of aspects of integration tests in ASP.NET Core using the xUnit test framework. However, the magical in xUnit creates confusion and difficulties for developers who are…


How to write concise code using Tuples as DTOs

Image for post
Image for post

In this article, I will show you how tuples can be used to save some coding in certain scenarios. The idea is simple, but I don’t see similar code much, so I think it’s worth writing a short article to introduce it to more people.

Data Transfer Object

A data transfer object (DTO) is an object that carries data between processes. In a lot of articles, DTOs are described as simple containers for data to be transferred across system boundaries. The key signature of a DTO is that it does not contain any business logic or behavior.

For example, when we query a…


How to create an Angular component for uploading files to a .NET API endpoint

Image for post
Image for post

In this article, we will go through details of creating an Angular component for uploading files to a .NET Web API endpoint. The following screen recording shows the demo app.


How to quickly set up test data in unit tests

Image for post
Image for post
The builder icon is made by Good Ware from www.flaticon.com; The MOQ icon is from its Git repository.

We regularly need to create some test data (or objects) for unit tests. A lot of unit tests only utilize a part of the input object and disregard the rest of the properties of the data object. For example, if a object contains a name, gender, birth date (and so on) and we want to write a unit test to check the calculation of a person’s age, we know that a person’s age only depends on the current date and the person’s birth date, and the person’s other attributes do not matter in this specific unit test. …


Let’s build an Angular app with JWT authentication.

Image for post
Image for post

In my last article, JWT Auth in ASP.NET Core, we talked about the implementation of JWT in the back-end. To follow up, this article will focus on the front-end part of the JWT story. You can find the front-end source code from the same GitHub repository as the back-end part.

To make JWT authentication work, the front-end application at least operates in the following scenes:

  1. Displays a login form, and sends user credentials to the back-end service to get user’s claims, a JWT access token, and a refresh token.
  2. Stores the JWT access token and refresh token in a browser’s…


How to implement JWT authentication and authorization in ASP.NET Core

Image for post
Image for post

JSON Web Token (JWT) is a compact and URL-safe string, which represents claims in a specific format that is defined by the industry-standard RFC 7519. JWT is a standard method to transmit claims securely between two parties. People typically use JWTs as identity proof in web applications and mobile apps.

There are many articles talking about JWT, and I think JWT is really being hyped. As a responsible writer, I would strongly encourage you to thoroughly consider whether you should use JWT at all. The following articles are worth reading in order to make an educated and unbiased decision.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store