1

I have had a look at this page

HTML - pick images of Root Folder from Sub-Folder

This page contains an example for HTML images. I have created a web control that represents footer of all the website pages. Now some pages are directly in the root and some are in another folder. In the web control I have to fix the

So without switching between ../ or ./ how can I determine the root of the website and then say something like root/images/abcd.jpg in the web control?

Community
  • 1
  • 1
Azhar Khorasany
  • 2,712
  • 16
  • 20
  • Use `~/` to get the application root. – Oded Aug 28 '12 at 14:00
  • This ~/ don't work at all neither in the main page (directly on the root) nor in the page inside a folder :( – Azhar Khorasany Aug 28 '12 at 14:02
  • @AzharKhorasany If Oded's suggestion doesn't work for you is because you need to make the `img` tag a server control by adding `runat="server"`. I explained this on my answer. – Icarus Aug 28 '12 at 14:17

2 Answers2

2

If you make your <img> tag a server control by adding runat="server" you can simply do the following and it will always resolve the url correctly, regardless of where the user controls is referenced:

<img id="image" src='<%=ResolveClientUrl("~/images/abc.jpg")' />

The ResolveClientUrl method will automatically resolve the correct path starting off from the root of the app (~/)

Icarus
  • 63,293
  • 14
  • 100
  • 115
1
var url = HttpContext.Current.Request.Url;
return url.AbsoluteUri.Replace(url.AbsolutePath, "") + "/";

To get the root.

Attila Szasz
  • 3,033
  • 3
  • 25
  • 39