Our great sponsors
-
ASP.NET Core
ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
-
swagger-ui
Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
But aren't we talking about using an off the shelf solution (i.e. provided by asp.net core)? It's just not easy to use compared to other frameworks.
You either have to do it manually or use a code generator (there are nugets for that, for example: https://github.com/Cysharp/UnitGenerator), but you absolutely can.
[Route("projects/{projectId:int}/files", Name = "UploadFile")] [Consumes("multipart/form-data")] [DisableFormValueModelBinding] [RequestSizeLimit(--num - bytes--)] [RequestFormLimits(MultipartBodyLengthLimit = --num - bytes--)] public async Task < IActionResult > PostAsync([FromRoute] int projectid) { // see nuget for https://github.com/ma1f/uploadstream var model = await this.StreamFiles < MyFileItem > (async file => { // never trust the client var fileName = file.FileName.GetSafeUniqueUri();
// wrapper around something like this: https://github.com/AJMitev/FileTypeChecker var fileType = await ExtractActualFileTypeFromFileHeadersOrTrustTheMimeType(file); // stream to blob storage var blobClient = blobContainerClient.GetBlobClient(fileName); var blob = new CloudBlockBlob(blobClient.Uri); await blob.UploadFromStreamAsync(file); await blobClient.SetHttpHeadersAsync(new BlobHttpHeaders { ContentType = fileType });
The code below is now roughly from memory and my controller would usually look something like this (Swashbuckle or NSwag attribute hints removed). There are some gotchas with Swagger and SwaggerUI for supporting multipart/form-data which I haven't included here since they now be fixed, since it's been a while since I checked.