Reputation: 456
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?
Upvotes: 12
Views: 15199
Reputation: 9
2 ways
<img src="@Url.Content("~/Images/trash.svg")"> OR
<img src='/images/trash.svg'>
Upvotes: 1
Reputation: 1361
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.
Upvotes: 1
Reputation: 499002
You should be using the Url.Content
helper with the tilda ~
marker (which resolves to the root of the application):
src="@Url.Content("~/Images/backup5.jpg")"
Upvotes: 23
Reputation: 1073
@Url.Content("~/Images/backup5.jpg")
Seems like a decent way to do it.
Upvotes: 1