Secure File Uploads to ASP.NET Core Minimal API Endpoints
ASP.NET Core’s minimal APIs provide a streamlined way to build lightweight, high-performance APIs with fewer dependencies and boilerplate code. Unlike traditional controller-based approaches, minimal APIs let developers define routes and request handlers directly in the program’s startup configuration. This approach is ideal for small services, microservices, or scenarios where simplicity and speed are paramount. Over time, minimal APIs have added support for features like in-memory caching, parameter binding, authentication, logging, and testing, making them robust enough for production applications.
One common requirement in modern APIs is file uploading, whether for user-generated content, reports, or data ingestion. Minimal APIs make this task straightforward by allowing direct access to request objects and file streams, while also supporting security mechanisms like anti-forgery tokens to prevent malicious uploads. In this guide, we’ll walk through setting up an ASP.NET Core project to handle file uploads efficiently, providing examples compatible with Visual Studio 2022.
To get started, first create a new ASP.NET Core Web API project in Visual Studio 2022. Launch the IDE, click “Create new project,” and select the “ASP.NET Core Web API” template. During setup, choose .NET 9.0 as the framework and ensure that the “Use controllers” option is unchecked, since we’ll be working with minimal APIs. Leave authentication disabled, and skip optional features like Open API support, HTTPS configuration, and Docker for this simple example. After configuring the project name and location, click Create to generate the base project.
Once the project is set up, you can define minimal API endpoints for file uploads using the IFormFile interface. Files can be read directly from incoming requests, validated for size and type, and saved to the server or cloud storage. Anti-forgery tokens can be added to protect against cross-site request forgery attacks, ensuring uploads are secure. This combination of simplicity, performance, and security makes minimal APIs an effective choice for handling file uploads in modern ASP.NET Core applications.

