Getting Web App root directory with HTML / JS code?

  • Thread starter Thread starter Joel Leong
  • Start date Start date
J

Joel Leong

I wonder how I can refer or get my web app root directory
without using code behind.

Here is my scenario.

I created the folders below

--WebAppRoot
----common
----img (assume a file called myimage.jpg is inside)
----reports
------finance
------admin

I hosted the app @ http://www.myhost.com/WebApps/

If I want to refer to http://www.myhost.com/ root dir, I
can use /WebApps/img/myimage.jpg. I also can use relative
path ../img/myimages.jpg if the HTML file is in reports
folder and ../../img/myimages if I'm in Finance folder.

My problem is I created a ASCX control which to be used
by all the pages. This ASCX control will render an IMG
tag that use this image. How should I specify the src for
this IMG tag? I cannot hard code my image to /WebApps
since WebApps dir may change later.

What is the industrial practice? Use code behind? How
would the code behind looks like if that is the case.
 
You want to set you img tags to runat =server and then tchange the url to
have ~ in front of them. ~ will be replaced by the root url for your
application when compiled.

for example

<img runat="server" id="Img1" src="~/img/myimage.jpg">

this will output src="http://www.myhost.com/WebApps/img/myimage.jpg"

MattC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top