Return a filled Word template as a browser download. The zip includes template.docx with one paragraph: Hello {{ name }}!.
dotnet add package RecursiveAI.OneDocument.AllRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument; var app = WebApplication.Create(args); var path = Path.Combine( app.Environment.ContentRootPath, "template.docx"); app.MapGet("/letter", (string? name) => { using var template = File.OpenRead(path); using var output = new MemoryStream(); var data = new Dictionary<string, object?> { ["name"] = name ?? "Ada" }; Template.Render(template, data, output); const string docx = "application/" + "vnd.openxmlformats-officedocument." + "wordprocessingml.document"; return Results.File(output.ToArray(), docx, "letter.docx"); }); app.Run();
Unzip the example, then dotnet run.
To start from scratch instead, use dotnet new web, install the package above, paste the code into Program.cs, and put template.docx in the project folder. Type Hello {{ name }}! in a blank Word document and save it as template.docx to make your own.
Run the endpoint
Open the address printed by dotnet run with /letter appended. The browser downloads letter.docx, and Word shows:
Hello Ada!The response includes this download header:
Content-Disposition: attachment; filename=letter.docx; filename*=UTF-8''letter.docxIf the template file is missing, File.OpenRead throws before the endpoint can send a download; put the file in the app’s content root.
Variations
Fill the template with a name from the request
Append this path to the running app’s address instead:
/letter?name=GraceOpen the new letter.docx to see:
Hello Grace!Related recipes
- Create a Word document from a template in C#
- Validate a Word template before rendering
- Fill Word documents with mail merge in C#
The short answer
In an ASP.NET Core minimal API, open your DOCX template inside the endpoint and call Template.Render with the request data and a MemoryStream. Pass the generated bytes to Results.File with the Word MIME type and a .docx filename. The app reads the template from disk but doesn’t write a temporary output file. Each request gets its own streams and data.
How do you create a Word document from a template?
Start with an ordinary .docx containing the text and formatting you want to keep. Where the text should change, insert a tag such as {{ name }}. A dictionary entry with the key name supplies the replacement. In the example, ASP.NET Core binds the name query parameter, and the handler uses Ada when it’s omitted. In an app that generates letters from stored records, supply the record’s name to the dictionary instead.
Placeholders work in headers and footers as well as the body. The Word templating guide also describes which formatting a replacement takes: if a tag spans text runs, the inserted text takes the formatting of the run where the tag began. If data is missing, Template.Render fills the tag with empty text by default. To read the warnings, capture the list returned by Template.Render; this example doesn’t use it. If a blank value would make a letter unusable, use strict mode to make missing data throw a TemplateException before sending the file.
How do you return a Word document as a download in ASP.NET Core?
Use the byte-array overload of Results.File to send the generated bytes directly. The MIME type identifies the file as DOCX. The download filename tells ASP.NET Core to set Content-Disposition for an attachment, which gives the browser a filename to offer. Sending the document bytes as a file also means the client doesn’t have to decode a JSON or Base64 response.
Calling ToArray() copies the result into a byte array before the MemoryStream is disposed, so the file response can still send it after the handler returns. This keeps the code simple for small documents like the sample letter. Budget memory for both the buffer and its copy if you adapt the endpoint to larger documents or many concurrent downloads.