How to get file from path for minimal API

When working on minimal api, we can’t use the Image.FromFile() https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.fromfile?view=windowsdesktop-7.0 in minimal API. It doesn’t work because it has been remove. But there is another way.

You can use Results.File to return file to download from your Minimal APIs handler:

var mimeType = "image/png";
FileInfo f = new FileInfo("myImage.png");
var results = Results.File(f.FullName, contentType: mimeType);

The FileInfo is only to make sure we get the correct working path https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=net-7.0