What is the correct way to reference an image in an asp.net mvc 4 project?

Viewed 14018

I have image references that look like this: src="../../Images/backup5.jpg". This matches the project structure. Although in the published version, these links do not work. If i change the path to src="../Images/backup5.jpg" by removing a "../" the image will display correctly. What is the correct way to reference these images so they work in both the development version and in the published version?

Follow up, how should this look when used in css as a background: attribute?

5 Answers

For the benefit of those who are not working on an MVC project, they can use:

<img src="../Images/yourImage.png" height="yourHeight" width="yourWidth"/>

where Images is a new folder added to the existing project and yourImage.png can be added to that folder by doing right-hand click on the Images folder, selecting Add and then Existing item, and navigating to where the file is stored.

2 ways

<img src="@Url.Content("~/Images/trash.svg")">  OR 
<img src='/images/trash.svg'>
Related