---
title: Working with media libraries on MVC sites
related:
  - https://docs.kentico.com/k11/managing-website-content/working-with-files/media-library-files.md
  - https://docs.kentico.com/k11/managing-website-content/configuring-the-environment-for-content-editors/configuring-media-libraries.md
  - https://docs.kentico.com/k11/managing-website-content/configuring-the-environment-for-content-editors/configuring-media-libraries/media-libraries-security/assigning-permissions-to-media-libraries.md
  - https://docs.kentico.com/k11/running-kentico-on-microsoft-azure/configuring-azure-cdn.md
  - https://docs.kentico.com/k11/custom-development/working-with-physical-files-using-the-api/configuring-file-system-providers/configuring-amazon-s3.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).

Media libraries represent a data storage for your MVC application. They can store different types of audio, video, image, and document files. For a complete list of the file types supported by default, see [Supported file types in Media libraries](https://docs.kentico.com/k11/managing-website-content/configuring-the-environment-for-content-editors/configuring-media-libraries.md#supported-file-types-in-media-libraries).

You can retrieve media library files in MVC applications by using the same Kentico API methods as you would when developing custom Kentico features.

By default, content of media libraries is synchronized via [web farms](https://docs.kentico.com/k11/configuring-kentico/optimizing-website-performance/setting-up-web-farms.md) and duplicated between Kentico and your MVC application. If you need to store media files in one location, see [Configuring custom storage for media libraries](https://docs.kentico.com/k11/managing-website-content/configuring-the-environment-for-content-editors/configuring-media-libraries.md#configuring-custom-storage-for-media-libraries).

## Retrieving media library files in MVC applications

The following example demonstrates how to retrieve files from Kentico media libraries in MVC applications. The example requires a media library with the code name _SampleMediaLibrary_ to work properly.

> **Tip:** **Tip**: To view the full code of a functional example directly in Visual Studio, download the [Kentico MVC solution](https://github.com/Kentico/Mvc) from GitHub and inspect the **LearningKit** project. You can also run the Learning Kit website after connecting the project to a Kentico database.

1. Open your MVC project in Visual Studio.
2. Create a new controller class or edit an existing one.
3. Implement a GET action method to retrieve media files.\
   The controller action retrieves .jpg files from the _SampleMediaLibrary_ media library using the methods from the _CMS.MediaLibrary_ namespace.

   ```csharp

   using System.Collections.Generic;
   using System.Web.Mvc;

   using CMS.MediaLibrary;
   using CMS.SiteProvider;


   ```

   ```csharp title="Retrieving media files in a controller action"

               // Creates an instance of the 'SampleMediaLibrary' media library for the current site
               MediaLibraryInfo mediaLibrary = MediaLibraryInfoProvider.GetMediaLibraryInfo("SampleMediaLibrary", SiteContext.CurrentSiteName);

               // Gets a collection of media files with the .jpg extension from the media library
               IEnumerable<MediaFileInfo> mediaLibraryFiles = MediaFileInfoProvider.GetMediaFiles()
                   .WhereEquals("FileLibraryID", mediaLibrary.LibraryID)
                   .WhereEquals("FileExtension", ".jpg");


   ```

You can now pass the media library files to a view or, optionally, create a view model for media files and pass it instead.

## Displaying media library files on MVC sites

### Example

In order to display media library files on your MVC site, you need to generate URLs for the media files.

1. Open your MVC application in Visual Studio.
2. Install the **Kentico.MediaLibrary** and **Kentico.Content.Web.Mvc** NuGet [integration packages](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.
3. Create a view that displays the media library files. To resize image files, you can use the _ImageUrl(string path, SizeConstraint constraint)_ method.

   ```csharp title="Media file view example"

   @using Kentico.Content.Web.Mvc
   @using Kentico.MediaLibrary
   @using Kentico.Web.Mvc
   @using CMS.MediaLibrary

   @model IEnumerable<MediaFileInfo>

   <h2>Media library files listing</h2>

   @foreach (MediaFileInfo mediaFile in Model)
   {
       @* Gets a direct URL for the media file and limits the file's maximum side size to 400 px *@
       string url = Url.Kentico().ImageUrl(mediaFile.GetPermanentUrl(), SizeConstraint.MaxWidthOrHeight(400));

       <img src="@url" alt="@mediaFile.FileTitle" />
   }


   ```

The media library files are shown on the MVC site.

### Generating media library file URLs

The **Kentico.MediaLibrary** NuGet [integration package](https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) adds two extension methods to the _MediaFileInfo_ class:

- **GetUrl** – returns a direct URL to the media file, for example: _\~/Kentico/DancingGoat/media/Images/sample\_image.jpg_
- **GetPermanentUrl** – returns a permanent URL to the media file, for example: _\~/getmedia/0140bccc-9d47-41ea-94a9-ca5d35b2964c/sample\_image_

  > **Info:** You can use both methods to generate URLs for media libraries stored in Kentico.
  >
  > Use the _GetPermanentUrl_ method to allow checking of media library permissions. See [Assigning permissions to media libraries](https://docs.kentico.com/k11/managing-website-content/configuring-the-environment-for-content-editors/configuring-media-libraries/media-libraries-security/assigning-permissions-to-media-libraries.md) for more details.

The _ImageUrl_ method in the example uses the _MaxWidthOrHeight_ size constraint parameter to limit the maximum side size of the displayed image.

You can use the following size constraints:

- _SizeConstraint.Empty –_ leaves the image as-is
- _SizeConstraint.Height(100)_ – resizes the image to the specified height and maintains aspect ratio
- _SizeConstraint.MaxWidthOrHeight(100)_ – resizes the image so that its width and height do not exceed the specified value (maintains aspect ratio)
- _SizeConstraint.Size(100, 120)_ – resizes the image to the specified width and height, and does not maintain aspect ratio
- _SizeConstraint.Width(100)_ – resizes the image to the specified width \*maintains aspect ratio)

  > **Info:** **Note**: The resized images are never larger than the original.

To force a file download, you can use the _FileUrl(string path, FileUrlOptions options)_ method:

```csharp

    @using CMS.MediaLibrary
    @using Kentico.Web.Mvc
    @using Kentico.MediaLibrary
    @using Kentico.Content.Web.Mvc


```

```csharp

    // Gets a direct URL for the media file and forces download
    Url.Kentico().FileUrl(mediaFile.GetUrl(), new FileUrlOptions() { AttachmentContentDisposition = true })


```

## Media libraries in an external storage

To store media library files in an external storage (cloud-based file system), you need to:

1. Configure your MVC application to use the given external storage provider for the media library folder. The instructions for configuring MVC applications are the same as for Kentico projects.

   - For Azure Blob storage, see [Configuring Azure storage](https://docs.kentico.com/k11/custom-development/working-with-physical-files-using-the-api/configuring-file-system-providers/configuring-azure-storage.md#configuring-kentico-to-store-only-media-files-on-azure-storage).
   - For Azure CDN, see [Configuring Azure CDN](https://docs.kentico.com/k11/running-kentico-on-microsoft-azure/configuring-azure-cdn.md).
   - For Amazon S3, see [Configuring Amazon S3](https://docs.kentico.com/k11/custom-development/working-with-physical-files-using-the-api/configuring-file-system-providers/configuring-amazon-s3.md).
2. In the views that display the media library files, always generate URLs by calling the **GetPermanentUrl** extension method for _MediaFileInfo_ objects.

> **Note:** **Note**: URLs generated by the _GetUrl_ method for external storage files do not work on MVC applications.
