---
title: "Generate a Word document from a template in ASP.NET Core"
url: https://recursiveai.org/onedocument/cookbook/download-filled-word-template-in-aspnet-core/
description: "Fill a Word DOCX template from an ASP.NET Core minimal API in C# and return the generated document as a browser download without writing an output file."
version: "1.0.0"
---

Return a filled Word template as a browser download. The zip includes `template.docx` with one paragraph: `Hello {{ name }}!`.

```shell
dotnet add package RecursiveAI.OneDocument.All
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onedocument/cookbook/download-filled-word-template-in-aspnet-core/download-filled-word-template-in-aspnet-core.zip), 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:

```text
Hello Ada!
```

The response includes this download header:

```text
Content-Disposition: attachment; filename=letter.docx; filename*=UTF-8''letter.docx
```

If 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:

```text
/letter?name=Grace
```

Open the new `letter.docx` to see:

```text
Hello Grace!
```

## Related recipes

- [Create a Word document from a template in C#](https://recursiveai.org/onedocument/cookbook/fill-word-template/)
- [Validate a Word template before rendering](https://recursiveai.org/onedocument/cookbook/validate-word-template-before-rendering/)
- [Fill Word documents with mail merge in C#](https://recursiveai.org/onedocument/cookbook/mail-merge/)

## 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](https://recursiveai.org/onedocument/docs/templating-docx/) 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`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.results.file?view=aspnetcore-10.0) 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.
