---
title: Working with page attachments in MVC applications
related:
  - https://docs.kentico.com/k9/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-objects.md
  - https://docs.kentico.com/k9/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/k9/managing-website-content/working-with-files/page-attachments.md) represent data, for example images, stored in [page type](https://docs.kentico.com/k9/developing-websites/defining-website-content-structure/page-types.md) fields. Attachments are stored in the following [data types](https://docs.kentico.com/k9/custom-development/developing-form-controls/reference-field-editor.md) of page type fields:

- _Attachments –_  allows you to store multiple attachments in a single page type field.
- _File_ – allows you to store a single page attachment in a page type field.

Kentico provides a standard API for working with attachments within the Kentico application. For working with attachments in MVC applications, Kentico provides a different API. The API is part of the code that you can [generate](https://docs.kentico.com/k9/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-objects.md) for individual page types.

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

\*\*Example of working with page attachments using [generated](https://docs.kentico.com/k9/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 single attachment in the retrieved article's 'Teaser' field.
var teaserImageWidth = article.Fields.Teaser.ImageWidth;

...

// Iterates over attachments in the article's 'Images' field.
foreach (var image in article.Fields.Images)
{
    var imageMimeType = image.MimeType;
    ...
}

```

## Creating page attachment URLs

1. [Perform the initial set up](https://docs.kentico.com/k9/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development.md) of your MVC application.
2. Use the _Url.Kentico().Attachment()_ method to retrieve page attachment URLs.

   ```csharp

   <img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser)">

   ```
3. The result is a relative URL in the following format:

   ```text

   ~<domain>/getattachment/<file GUID>/<filename>
   ```
4. See also:

Because the returned URLs are relative, the MVC site needs to run on the same domain as the site configured in Kentico. Another solution is setting up a [domain alias](https://docs.kentico.com/k9/configuring-kentico/managing-sites/setting-domain-names-for-sites.md) leading to the MVC site.

**Note**: the system processes the file names and replaces all special characters with a '-' (hyphen). All characters that are not _0-9_, _a-z_ (latin small letter a to latin small letter z), '_._' (full stop) or '_\__' (underscore) are replaced.

### Attachment versioning

The _Url.Kentico().Attachment()_ method always returns the attachment that is attached to the version of the page you are currently working with. For example, using the _Url.Kentico().Attachment()_ method on pages retrieved using the [generated code](https://docs.kentico.com/k9/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-objects.md) providers always retrieves the attachment belonging to the published version of the page (the providers work with published versions of pages).

### Constraining the returned attachment size for image attachments

You can further parametrize the Url.Kentico().Attachment method to retrieve attachments of specific size:

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

```csharp

<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.Height(200))">

```

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

```csharp

<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.Width(200))">

```

Specify both the maximum width and height of the retrieved image. This constraint does not retain the original aspect ratio.

```csharp

<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.Size(400, 1200))">

```

Specify the maximum width and height that the retrieved image cannot exceed. This constraint retains the original aspect ratio and the resized image is never made larger than the original.

```csharp

<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.MaxWidthOrHeight(400))">

```

### Prompting users for action when accessing attachments

You can specify whether the web browser will prompt users to perform an action, such as saving the file to a disk, when accessing attachments.

```csharp

@using Kentico.Web.Mvc

@{
var urlOptions = new AttachmentUrlOptions { AttachmentContentDisposition = true }; 
}

<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.Size(1280, 800), urlOptions))">

```
