---
title: "How Can ASP.Net MVC controller return an Image?"  
description: "How Can ASP.Net MVC controller return an Image?"  
author: "Mark M"  
published: 2015-01-22  
updated: 2015-01-22  
canonical: https://www.mindstick.com/forum/12890/how-can-asp-dot-net-mvc-controller-return-an-image  
category: "asp.net"  
tags: ["asp.net mvc", "controller"]  
reading_time: 1 minute  

---

# How Can ASP.Net MVC controller return an Image?

Can I create a [Controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) that simply returns an [image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) asset?

I would like to [route](https://www.mindstick.com/interview/396/what-is-routed-event-in-wpf) this [logic](https://www.mindstick.com/articles/331924/best-ways-to-improve-your-logic-building-skills-for-programming) through a controller, whenever a [url](https://www.mindstick.com/forum/436/url-redirection) such as the following is requested:

www.mywebsite.com/[resource](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health)/image/topbanner

The controller will look up topbanner.png and send that image directly back to the client.

I've seen examples of this where you have to create a [View](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view) - I don't want to use a View. I want to do it all with just the Controller.

Is this possible?

## Replies

### Reply by Allen Scott

Use the base controllers File method.

```
public ActionResult Image(string id){    var dir = Server.MapPath("/Images");    var path = Path.Combine(dir, id + ".jpg");    return base.File(path, "image/jpeg");}
```

As a note, this seems to be fairly efficient. I did a test where I requested the image through the controller (http://localhost/MyController/Image/MyImage) and through the direct url (http://localhost/Images/MyImage.jpg) and the results were:

- **MVC:** 7.6 milliseconds per photo
- **Direct:** 6.7 milliseconds per photo

Note: this is the average time of a request. The average was calculated by making thousands of requests on the local machine so the totals should not include network latency or bandwidth issues.


---

Original Source: https://www.mindstick.com/forum/12890/how-can-asp-dot-net-mvc-controller-return-an-image

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
