---
title: Working with page attachments in MVC applications
related:
  - https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-objects.md
  - https://docs.kentico.com/k11/managing-website-content/working-with-files/page-attachments.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

[Attachments](https://docs.kentico.com/k11/managing-website-content/working-with-files/page-attachments.md) represent files, for example images, stored in [page type](https://docs.kentico.com/k11/developing-websites/defining-website-content-structure/page-types.md) fields. Attachments can be stored in the following [data types](https://docs.kentico.com/k11/custom-development/developing-form-controls/reference-field-editor.md) of page type fields:

- **Attachments** – multiple attachments in a single page type field.
- **File** – a single page attachment.

For working with page attachments in MVC applications, we recommend using the API of the classes that you [generate](https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-objects.md) for individual page types. The generated code uses the **Attachment** type (available in the _CMS.DocumentEngine_ namespace) for _File_ fields, and **IEnumerable** for _Attachments_ fields.

The generated API gives you more control than the standard attachment API when creating URLs for page attachments. You can create strongly typed views, and the provided methods for creating page attachment URLs is independent of Kentico's settings. That is, the resulting URL is always in the same format and Kentico's image resizing settings are not applied.

> **Info:** To work with page attachments, you need to install the **Kentico.Content.Web.Mvc** [integration package](https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) into your MVC project.

**Example of working with page attachments using [generated](https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-objects.md)page type code**

```csharp

// Retrieves an article
Article article = ArticleProvider.GetArticle(nodeGuid, "en-US", "MySite");

// Accesses a property of an attachment stored in the article's 'Teaser' field (File data type)
int teaserImageWidth = article.Fields.Teaser.ImageWidth;

// Iterates over attachments in the article's 'Images' field (Attachments data type)
foreach (Attachment image in article.Fields.Images)
{
    string imageMimeType = image.MimeType;
    ...
}

```

## Creating page attachment URLs

1. [Perform the initial set up](https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development.md) of your MVC application.
2. Call the **GetPath** extension method (available in the _Kentico.Content.Web.Mvc_ namespace) to retrieve page attachment URLs:

   ```csharp

   string attachmentUrl = article.Fields.Teaser.GetPath();

   ```

   > **Info:** **Image variants**
   >
   > To get URLs for [image variants](https://docs.kentico.com/k11/developing-websites/managing-responsive-images/defining-image-variants.md) of page attachments, you need to specify the image variant definition identifier when calling the _GetPath()_ method.
   >
   > ```csharp
   >
   > string attachmentUrl = article.Fields.Teaser.GetPath("Mobile");
   >
   > ```

The result is a relative URL in the following format:

```text

~/getattachment/<file GUID>/<filename>
```

> **Info:** **Special characters**
>
> The system replaces all special characters in file names with a '-' (hyphen). This includes characters that are not 0-9, a-z (Latin lower case 'a' to 'z'), '.' (full stop) or '\_' (underscore).

 Because the returned URLs are relative, you need to resolve them for your website by calling the **ImageUrl** or **FileUrl** extension method in your views.

```xml title="Example"

<img alt="Article @article.Fields.Title" src="@Url.Kentico().ImageUrl(article.Fields.Teaser.GetPath(), SizeConstraint.Empty)">

```

See also:

### Attachment versioning

The _GetPath()_ extensionmethod always returns the attachment that is attached to the version of the page you are currently working with. For example, using the _GetPath()_ extension method on pages retrieved via a [generated provider class](https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-objects.md) retrieves the attachment belonging to the published version of the page (the providers work with published versions of pages by default).

### Constraining the returned attachment size for images

You can use the **Url.Kentico().ImageUrl** method to resize image attachments:

- Specify the maximum height of the retrieved image. This constraint retains the original aspect ratio.

  ```csharp

  Url.Kentico().ImageUrl(attachmentRelativeUrl, SizeConstraint.Height(200))

  ```
- Specify the maximum width of the retrieved image. This constraint retains the original aspect ratio.

  ```csharp

  Url.Kentico().ImageUrl(attachmentRelativeUrl, SizeConstraint.Width(200))

  ```
- Specify the maximum width and height that the retrieved image cannot exceed. This constraint retains the original aspect ratio.

  ```csharp

  Url.Kentico().ImageUrl(attachmentRelativeUrl, SizeConstraint.MaxWidthOrHeight(400))

  ```
- Specify the exact width and height of the retrieved image. This constraint does **not** retain the original aspect ratio. The resized image is never made larger than the original size.

  ```csharp

  Url.Kentico().ImageUrl(attachmentRelativeUrl, SizeConstraint.SizeConstraint.Size(400, 1200))

  ```

### Prompting users for action when accessing attachments

You can use the **Url.Kentico().FileUrl** method to create links that prompt users to save an attachment file to their disk:

```csharp

@using Kentico.Content.Web.Mvc

@{ 
var urlOptions = new FileUrlOptions() { AttachmentContentDisposition = true };
}

<img alt="Article @article.Fields.Title" src="@Url.Kentico().FileUrl(article.Fields.Teaser.GetPath(), urlOptions)">

```
