0

In IIS, I have my website set up like so:

- MyWebsite
    - Images
    - MyVirtualDirectory
        - Management

I am wanting to access image files in MyWebsite/Images from a page in MyVirtualDirectory/Management.

For example, in MyVirtualDirectory/Management/MyPage.aspx, I am trying to access an image like so:

Server.MapPath("~/Images/myimage.jpg")

But after running, the path is not correct. In google chrome I can view the HTML, and it appears as <img src="../../Images/myimage.jpg" />. If I remove the ../.. then it is fine, but I am wondering why the ~/... syntax isn't working.

sme
  • 3,435
  • 3
  • 22
  • 39

1 Answers1

1

The Server.MapPath("~/somepath") syntax is working as expected. This function returns the physical path while treating the "~" as the root directory of the current website. In your case, the current website is the MyVirtualDirectory, so the function is returning a physical path based on the root of this virtual directory. Check this answer for a detailed explanation here

Faruq
  • 1,024
  • 8
  • 23
  • Is it possible to get the code to work by changing settings in IIS, as opposed to changing any code? – sme Sep 26 '18 at 03:37