# Add CSV Export to a Blazor Web App

Yes. An agent adds a streaming CSV download endpoint to your Blazor Web App that honours the grid's filter, links it from the page, builds it, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-csv-export/blazor/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the app before it adds an endpoint. In an app laid out like the `BlazorWebAppMovies` sample in dotnet/blazor-samples, `BlazorWebAppMovies.csproj` targets `net10.0` with nullable and implicit usings on and references `Microsoft.AspNetCore.Components.QuickGrid.EntityFrameworkAdapter`, `Microsoft.EntityFrameworkCore.SqlServer`, `Microsoft.EntityFrameworkCore.Tools` and `Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore`, all at 10.0.0. `Program.cs` registers `BlazorWebAppMoviesContext` with `AddDbContextFactory` on the `BlazorWebAppMoviesContext` connection string, which `appsettings.json` points at SQL Server LocalDB, calls `AddQuickGridEntityFrameworkAdapter`, seeds five films through `SeedData.Initialize`, and maps Razor components with the interactive server render mode. `Models/Movie.cs` has `Id`, a `Title` of 3 to 60 characters, a `DateOnly` `ReleaseDate`, a `Genre` constrained by a regular expression, a `Price` stored as `decimal(18, 2)` and limited to 0 to 100, and a `Rating` that must be one of `G`, `PG`, `PG-13`, `R` or `NC-17`. `Components/Pages/MoviePages/Index.razor` at `/movies` uses `@rendermode InteractiveServer`, creates a context from the factory in `OnInitialized`, filters with `context.Movie.Where(m => m.Title!.Contains(titleFilter))` from a search box bound on `oninput`, and shows a `QuickGrid` with five sortable columns and a `Paginator` at five items per page. Three migrations are checked in.

An export should not go through the interactive circuit, where a large result would be rendered into a component and pushed over SignalR. The agent adds a minimal API endpoint in `Program.cs`, `app.MapGet("/movies/export.csv", ...)`, that takes an optional `title` query parameter, creates a context from the same `IDbContextFactory`, applies the same `Contains` filter, orders by `Title`, and streams rows to the response with a `StreamWriter` as it enumerates the query with `AsAsyncEnumerable`, so a table of any size is written without being loaded into memory. The response carries `text/csv; charset=utf-8` and a `Content-Disposition` of `attachment` with a filename that includes the date.

The CSV itself is written by a small static `CsvWriter` class in a new `Services/` folder rather than a package: a header row, then one row per film with `Id`, `Title`, `ReleaseDate` as `yyyy-MM-dd`, `Genre`, `Price` and `Rating`. Fields are quoted when they contain a comma, a quote or a line break, with quotes doubled, which matters because `Genre` allows parentheses and spaces and a `Title` can hold anything; numbers and dates are formatted with the invariant culture so the file does not change with the server's locale. The pull request names `CsvHelper` (34.0.0 on NuGet) as the choice for a team that wants type maps and reading as well.

On the page, the agent adds an anchor next to "Create New": `<a href="@($"movies/export.csv?title={Uri.EscapeDataString(titleFilter)}")" download>Export CSV</a>`, so the download carries whatever is in the search box. Because the page is interactive and the link points outside the Blazor router, the agent checks that enhanced navigation does not intercept it and sets `data-enhance-nav="false"` if it does. The connection string names LocalDB, which exists only on Windows, so the Linux sandbox cannot run the app against its database; the agent proves what it can: `dotnet build` with no warnings from the new code, `dotnet format --verify-no-changes`, and the writer run against the five seed films in a scratch program whose output it quotes, and it says in the pull request that the end-to-end download is for the reviewer on a machine with LocalDB or a SQL Server connection string.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a CSV export to this Blazor Web App. Read the csproj, Program.cs, appsettings.json, Models/Movie.cs, Data/BlazorWebAppMoviesContext.cs, Data/SeedData.cs and Components/Pages/MoviePages/Index.razor first. Add a minimal API endpoint GET /movies/export.csv in Program.cs that takes an optional title query parameter, uses the registered IDbContextFactory, applies the same Title Contains filter as the Index page, orders by Title, and streams rows with AsAsyncEnumerable to a StreamWriter with text/csv and an attachment Content-Disposition. Write a small static CSV writer under Services that quotes fields containing commas, quotes or line breaks, doubles quotes, formats ReleaseDate as yyyy-MM-dd and uses the invariant culture. Add an Export CSV link on Index.razor that passes the current filter, with data-enhance-nav false if enhanced navigation intercepts it. The connection string uses LocalDB, so do not try to run the database here: run dotnet build and dotnet format --verify-no-changes, exercise the writer against the seed films in a scratch program you do not commit, and open a pull request that quotes the build output and the sample CSV and says the download itself is for the reviewer to try.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes `Program.cs` and `Components/Pages/MoviePages/Index.razor` and adds `Services/CsvWriter.cs`. Its description quotes `dotnet build` and `dotnet format` from the sandbox, shows the CSV the writer produces for the five seed films (which demonstrates the quoting on the `Sci-fi (Cyberpunk)` genre and the `Mad Max: Fury Road` title), and states plainly that the endpoint was not exercised against a database in the sandbox and why.

A reviewer runs the app with LocalDB, opens `/movies`, types a filter and clicks the link, then opens the file in a spreadsheet to check the header, the date format and that the filter was applied. The reviewer also checks that the endpoint uses the factory and disposes the context, and whether it should sit behind authorisation once the app has any.

From then on a new column on `Movie` is one more field in the writer, and the same endpoint shape serves any other grid in the app.

## Run it on every Blazor app with a data grid

A team with several internal Blazor apps sends this body once per repository from its own server, each `POST /o/{org}/sessions` with that repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started. Each run reads its own app: one on SQLite or PostgreSQL gets the endpoint run and the download checked in the sandbox, and one with authorisation gets the endpoint placed behind it.

An automation on the Project with a `github` trigger on `pull_request.opened` and the prompt "if this pull request adds a column to a model that has a CSV export, check that the writer and the header include it and post a review" runs once per pull request. The trigger adds the pull request after the prompt and fills in nothing else. Only work by an owner, member or collaborator fires it.

## Why run it on Rush?

The two ways an export goes wrong, a file that breaks on the first comma in a title and a page that loads the whole table into a component before sending it, are both invisible on a five-row sample. The agent writes the quoting rules and the streaming query deliberately, proves the writer on real seed data and the build in its own sandbox, and is explicit about what it could not run, keeping every command and its output with the pull request.

A reviewer reads one endpoint, one writer and one link, and spends the saved time trying the download. A team with several apps starts the same request across all of them at once.

## Frequently asked questions

### Why a minimal API endpoint instead of a button in the component?

A file download from an interactive server component would have to pass bytes over the SignalR circuit through JavaScript interop. An endpoint streams the response directly from the query, works with a plain link, and is unchanged if the page later moves to another render mode.

### Does the export respect the search box?

Yes. The link carries the current `titleFilter` as a query parameter and the endpoint applies the same `Contains` filter the grid uses, so what is exported is what is listed, without the pagination.

### Why not CsvHelper?

Six columns with fixed formatting do not need a library, and a dependency-free writer is easier to review. The pull request names `CsvHelper` for a team that also wants to read CSV or map many types.

### Why wasn't the download tested in the sandbox?

The sample's connection string targets SQL Server LocalDB, which runs only on Windows. The agent proves the build, the formatting and the writer's output, and leaves the click-through to the reviewer; with a SQLite or container-based connection string the run would exercise the endpoint too.

## More Blazor jobs

- [Add Dark Mode to a Blazor Web App](https://getrush.ai/run/add-dark-mode/blazor/)
- [Add a Dockerfile to a Blazor Web App](https://getrush.ai/run/add-dockerfile/blazor/)
- [Set Up GitHub Actions CI for a Blazor Web App](https://getrush.ai/run/add-github-actions-ci/blazor/)

## The same job on other stacks

- [Add CSV Export to a Django App from a Linear Issue](https://getrush.ai/run/add-csv-export/django/)
- [Add CSV Export to a Django REST Framework API](https://getrush.ai/run/add-csv-export/django-rest-framework/)
- [Add CSV Export to a Quarkus REST API](https://getrush.ai/run/add-csv-export/quarkus/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
