how to specify relate path from application root

Discussion in 'ASP .Net' started by Ben Daniel, Oct 13, 2007.

  1. Ben Daniel

    Ben Daniel Guest

    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
    Ben Daniel, Oct 13, 2007
    #1
    1. Advertising

  2. 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>
    Michael Nemtsev, MVP, Oct 13, 2007
    #2
    1. Advertising

  3. Ben Daniel

    Roland Dick Guest

    Hi Ben,

    Ben Daniel schrieb:
    > 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
    Roland Dick, Oct 13, 2007
    #3
  4. "Roland Dick" <> wrote in message
    news:...

    > This was for an embed-Tag to embed a flash file, but I would think it also
    > works for a JS reference.


    It does.


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 13, 2007
    #4
  5. Ben Daniel

    Ben Daniel Guest

    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.
    Ben Daniel, Oct 14, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keithb
    Replies:
    1
    Views:
    2,713
    Teemu Keiski
    Apr 8, 2006
  2. Jerry
    Replies:
    10
    Views:
    497
  3. Li Daobing
    Replies:
    1
    Views:
    382
    Tom Widmer
    Jun 21, 2006
  4. markla
    Replies:
    1
    Views:
    531
    Steven Cheng
    Oct 6, 2008
  5. Navin
    Replies:
    1
    Views:
    669
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page