# VolumeResourceBuilderExtensions Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [VolumeResourceBuilderExtensions](/reference/api/csharp/aspire.hosting/volumeresourcebuilderextensions.md)
- Kind: `Methods`
- Members: `1`

Provides extension methods for adding volume-backed storage to compute resources.

<a id="withvolume"></a>
<a id="withvolume-iresourcebuilder-t-string-string-string-bool"></a>

## WithVolume(IResourceBuilder<T>, string, string, string, bool)

- Name: `WithVolume(IResourceBuilder<T>, string, string, string, bool)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/f4c27f2d43ddc1cacd0dd083b30d1fea1cee7a62/src/Aspire.Hosting/VolumeResourceBuilderExtensions.cs#L47-L52)

Adds a volume to a compute resource and exposes its effective path through an environment variable.

```csharp
public static class VolumeResourceBuilderExtensions
{
    public static IResourceBuilder<T> WithVolume<T>(
        this IResourceBuilder<T> builder,
        string name,
        string target,
        string env,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `name` (`string`)
  The name of the volume.
- `target` (`string`)
  The target path where the volume is mounted after publishing.
- `env` (`string`)
  The environment variable that receives the effective volume path.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if the published volume should be mounted as read-only.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

Containers receive `target` in run and publish modes. Projects and executables receive a workload-scoped [IAspireStore](/reference/api/csharp/aspire.hosting/iaspirestore.md) directory in run mode and `target` in publish mode. Named storage is independent of the resource lifetime. Session resources stop with the AppHost and reuse their named storage on the next run; persistent resources can keep the compute instance alive and continue using the same storage. Cleaning the AppHost store can remove local project and executable data.

## Examples

```csharp
builder.AddProject<Projects.Api>("api")
    .WithVolume("data", "/usr/data", env: "DATA_PATH");
```

## ATS metadata

### Ignored by ATS

- Reason: Polyglot export is via CoreExports.WithVolume which reorders parameters.
