liveBook · Manning

FileResult

As explained in .NET in Action, Second Edition

A File ActionResult is an ASP.NET Core action result that sends file or binary content in an HTTP response.

A FileResult sends generated file or binary content in an HTTP response. In an ASP.NET Core endpoint, Results.File accepts encoded bytes and a content type. section 3.1.2

Returning a barcode image

A route can generate a barcode and return it as JPEG data: app.MapGet("/barcode/{text}", (string text) => { var barcode = new Barcode(); barcode.ImageFormat = ImageFormat.Jpeg; _ = barcode.Encode(TYPE.CODE128, text, Color.Black, Color.White); return Results.File(barcode.Encoded_Image_Bytes, "image/jpeg"); }); The content type must match the generated image format, and barcode input restrictions may require validation. section 3.1.2

using BarcodeLib;                                    #1
using System.Drawing;                                #2
using System.Drawing.Imaging;                        #3
  
app.MapGet("/barcode/{text}", (string text) => {     #4
  var barcode = new Barcode();                       #5
  barcode.ImageFormat = ImageFormat.Jpeg;            #6
  _ = barcode.Encode(                                #7
    TYPE.CODE128,                                    #8
    text,                                            #9
    Color.Black,                                     #10
    Color.White);                                    #11
  return Results.File(                               #12
    barcode.Encoded_Image_Bytes,                     #13
    "image/jpeg");                                   #14
});

The listing shows the complete endpoint flow: configure BarcodeLib, encode request text, and return image bytes with an image/jpeg content type. section 3.1.2

How .NET in Action, Second Edition covers it

How deep it goes
Demonstrates encoded image bytes and MIME-type selection, but stops short of explaining overloads or response internals.
Examples
Uses BarcodeLib to encode route text as a CODE128 JPEG returned through Results.File.
What it assumes
Assumes an ASP.NET Core web API and package-reference workflow, then applies them to a barcode response.
What you can do after
Build it from scratch by mapping an endpoint that returns generated image bytes through Results.File.
How it connects
Connects action results with endpoint mapping, barcode generation, image formats, MIME types, and binary response content.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.