---
title: Displaying page attachments
related:
  - https://docs.kentico.com/k12sp/developing-websites/generating-classes-for-kentico-objects.md
  - https://docs.kentico.com/k12sp/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/k12sp/managing-website-content/working-with-files/page-attachments.md) represent files, for example images, stored in [page type](https://docs.kentico.com/k12sp/developing-websites/defining-website-content-structure/creating-and-configuring-page-types.md) fields. Page attachments can be stored in fields of the following [data types](https://docs.kentico.com/k12sp/custom-development/developing-form-controls/reference-field-editor.md):

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

For working with page attachments in MVC applications, we recommend using the API provided by classes that you [generate](https://docs.kentico.com/k12sp/developing-websites/generating-classes-for-kentico-objects.md) via the **Code** tab of individual page types. The generated code uses the **DocumentAttachment** 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 resolving URLs for page attachments. The following example illustrates how to access various page attachments of the _Article_ page type using its corresponding generated provider class.

```csharp title="Working with page attachments using generated page type code"

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

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

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

```

## Prerequisite

To ensure that attachment URLs work correctly in both the MVC application and the connected Kentico administration interface, the system must be configured to use the permanent URL format. MVC sites have permanent URLs **enabled by default**, but you can use the following steps to confirm the configuration:

1. Access the administration interface of your Kentico application.
2. Open the **Settings** application.
3. Select the **URLs and SEO** category.
4. Make sure the **Use permanent URLs** setting is enabled.

With permanent URLs enabled, the system generates valid URLs in a consistent format for all possible types of attachments.

## Creating page attachment URLs

To generate URLs for **DocumentAttachment** objects, call the **GetPath** extension method (available in the _Kentico.Content.Web.Mvc_ namespace):

```csharp

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

```

> **Info:** **Image variants**
>
> To get URLs for [image variants](https://docs.kentico.com/k12sp/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>
```

The system automatically resolves relative URLs to their absolute form using a filter that processes the output of all pages.

> **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).

We recommend that you further adjust the URLs by calling the **ImageUrl** or **FileUrl** extension method in your views. The methods allow you to set size constraints for images or the disposition for file links.

```xml title="Example"

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

```

See also:

### Page 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/k12sp/developing-websites/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)">

```
