how to specify relate path from application root

B

Ben Daniel

Hi all,

I think this question should be fairly common but I couldn't find any
existing answers in the forums.

I have a Master Page that refers to uses some client scripts like so:

<script language="javascript" type="text/javascript" src="Scripts/
Browser.js" />
<script language="javascript" type="text/javascript" src="Scripts/
Common.js" />

So any pages that inherit from this master page will automatically use
these scripts. And it works fine for pages in the application's root
directory BUT but all the pages I have under an \Admin\ directory
don't work as it tries to find the scripts under /WebsiteRoot/Admin/
Scripts/ instead of /WebsiteRoot/Scripts/

It seems I can't use "~" either! e.g. src="~/Scripts/Browser.js"

I reckon this would have to be a common problem encountered by asp.net
developers! So I'm desperate to find out how others (and how I can)
get around this problem.

Thanks in advance, Ben
 
M

Michael Nemtsev, MVP

Hello Ben,

why not to specify the full path to your files from the root directory?


---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


BD> Hi all,
BD>
BD> I think this question should be fairly common but I couldn't find
BD> any existing answers in the forums.
BD>
BD> I have a Master Page that refers to uses some client scripts like
BD> so:
BD>
BD> <script language="javascript" type="text/javascript" src="Scripts/
BD> Browser.js" />
BD> <script language="javascript" type="text/javascript" src="Scripts/
BD> Common.js" />
BD> So any pages that inherit from this master page will automatically
BD> use these scripts. And it works fine for pages in the application's
BD> root directory BUT but all the pages I have under an \Admin\
BD> directory don't work as it tries to find the scripts under
BD> /WebsiteRoot/Admin/ Scripts/ instead of /WebsiteRoot/Scripts/
BD>
BD> It seems I can't use "~" either! e.g. src="~/Scripts/Browser.js"
BD>
BD> I reckon this would have to be a common problem encountered by
BD> asp.net developers! So I'm desperate to find out how others (and how
BD> I can) get around this problem.
BD>
BD> Thanks in advance, Ben
BD>
 
R

Roland Dick

Hi Ben,

Ben said:
I have a Master Page that refers to uses some client scripts like so:

<script language="javascript" type="text/javascript" src="Scripts/
Browser.js" />
these scripts. And it works fine for pages in the application's root
directory BUT but all the pages I have under an \Admin\ directory
don't work as it tries to find the scripts under /WebsiteRoot/Admin/
Scripts/ instead of /WebsiteRoot/Scripts/

It seems I can't use "~" either! e.g. src="~/Scripts/Browser.js"

That's right, "~" is an ASP.NET expression and only works in ASP.NET
controls.

I faced this problem recently, and I simply added an ASP.NET Literal
control instead of the HTML tag and set the text of the literal to the
complete HTML tag in code behind. Depending on the request, it adds
"../" in front of the path.
This was for an embed-Tag to embed a flash file, but I would think it
also works for a JS reference.

Hope this helps,

Roland
 
B

Ben Daniel

Thanks for your help everyone.

What I ended up doing was looking at the code behind the asp:image
control to see how it resolves the url in the ImageUrl property. It
calls a ResolveClientUrl function defined in Control. So I ended up
just doing this:

<script language="javascript" type="text/javascript" src="<%=
ResolveClientUrl("Scripts/Browser.js") %>" />
<script language="javascript" type="text/javascript" src="<%=
ResolveClientUrl("Scripts/Common.js") %>" />

Which works a treat. Thanks anyway.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top