Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to deploy my ASP.NET MVC site to some location C:\home\MySite\, and I want to give it a config value to point to a different location on that server: C:\home\SomeOtherLocation\Albums, and display images from that location on my page.

My controller returns a list of AlbumLinks, which contain a property called ImagePath, which looks like "C:\home\SomeOtherLocation\Albums\Album1\Image1.jpg". My view looks like this:

@model IEnumerable<AlbumLink>

@{
    ViewBag.Title = "Albums";
}

@foreach (AlbumLink l in Model)
{
    <p><img src="@l.ImagePath" /></p>
}

But the image is not displayed. I verified that the image is in the right location. Is this the proper way to display an external image on a View? I know that I can add the album into the solution along with all its images, but I don't want to do that. I want to deploy the site and configure it to point to a collection of images to display. I know it's doable. What's the best way to achieve this? Thanks.

share|improve this question

2 Answers

You cannot have links to files like C:\something it is because browser cannot access such path. All images must be hosted by your webserver.

You should use something like Server.MapPath.

share|improve this answer

2 problems here:

Firstly, you can't use ImagePath, as the path is local to the server, not the client. You need to provide the full URL

Second, IIS cannot see directories out of your directory structure. You can add a directory within IIS that redirects to the location like

  • Root
    • MySite -> c:\home\MySite
    • SomeOtherLocation -> c:\home\SomeOtherLocation

MVC will not find a matching controller and so will fall back to IIS to find the content.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.