5

I'm having trouble with an <img> element in a master page that I can't get to be reachable from both the home default.aspx page, and any nested page using the same src path.

For example, from the home page (located on the root), I'm able to display the image with:

<img src="Images/test.jpg">

But all pages located one level down, in a folder on the root, must use src="../Images/test.jpg" to display the image.

I have tried using ~ and other ways to get one path to work for both locations, but no luck yet.

This seems too easy, so I must be making this more difficult than it is... Feel free to enlighten me with any clues.

daaawx
  • 3,273
  • 2
  • 17
  • 16
Vince
  • 81
  • 1
  • 2
  • 3
  • Possible duplicate of [HTML - pick images of Root Folder from Sub-Folder](http://stackoverflow.com/questions/3655059/html-pick-images-of-root-folder-from-sub-folder) – Liam Nov 21 '16 at 16:32

3 Answers3

21

You can use

<img src="/Images/test.jpg"> 

in all the pages.

What ever be the page, image will be brought from url www.yourdomian.com/Images/test.jpg

Venkat Kotra
  • 10,413
  • 3
  • 49
  • 53
  • Thanks for the reply, but that path has just the opposite effect, as no pages can display the image.... I tried many different path strings but still no luck. Hoping someone else has had this problem and can offer a solution... – Vince Feb 15 '13 at 18:47
  • After more research, I found that this issue seems to occur when using the built-in asp.net development server, but works ok when using WebMatrix to launch the site... not sure why this is, but I'm hoping all will be well when I upload to the host... will update once I have it up... – Vince Feb 15 '13 at 20:37
  • Update - everything works fine after uploading to the web server. – Vince Feb 20 '13 at 10:56
  • If the sample url is : http://website.com/myapp/default.aspx ;; "./images/test.jpg" or "images/test.jpg" = http://website.com/myapp/images/test.jpg ;; "/images/test.jpg" = http://website.com/images/test.jpg – Jin Ginusuke Feb 15 '22 at 10:16
  • just what I needed to know; working well for me now – MikeDev Jun 09 '22 at 18:25
3

Consider:

@{    
   var src == Url.Content("~/images")+"/myImage.jpg";  
 }

Which also helps me to resolve the path ../ and ../../ in jquery calendar image display in IE.

arghtype
  • 4,376
  • 11
  • 45
  • 60
RPSarathi
  • 49
  • 5
-3

In my case - c# asp.net mvc (before publishing) - I worked this workaround as also no src combination did work for me.

Somewhere on the beginning of my View

@{
  var path = Server.MapPath("~/Images");
  var src = path + "\\myImage.jpg";  
 }

Later (still on the View) for the image

<img src="@src" />

Hope this helps someone :)

nevo
  • 47
  • 2
  • 7