Decrease Font Size
Increase Font Size

Method to resolve URL, resolve files path correctly

Sometimes we might run into an issue where the images or Style Sheet or JavaScript etc… not render correctly.

It could be very frustrating and we don’t want to hard code the absolute path to the destination files. Here are a few demonstrations on how to handle these types of situations. This is the structure of my web project.

default namespace

I have these lines in my master page. The script will render correctly on the Default.aspx page. But it will not behave the same if we have a Default.aspx page in the test folder

<script language="javascript" type="text/javascript"  src="js/ddaccordion.js"></script>
<script language="javascript" type="text/javascript" src=" js/jquery-1.2.2.pack.js"></script>
<iframe id="ads_frame"  marginheight="0" marginwidth="0"  scrolling="no" frameborder='0' src="slideAds.html"> </iframe> 

Here are a few tricks I find to remedy the problem. We can use the "string Control.ResolveClientUrl(string relativeUrl)" method

<script language="javascript" type="text/javascript" src='<%= ResolveClientUrl("~/js/ddaccordion.js") %>'></script>
 <iframe id="ads_frame"  marginheight="0" marginwidth="0"  scrolling="no" frameborder='0' src='<%= ResolveClientUrl("~/slideAds.html") %>'> </iframe> 

Another alternative is to use Request.ApplicationPath + "location if the file". But make sure you leave no space between the Request.ApplicationPath and the "location if the file".

Note: this will not work because of the space

<script language="javascript" type="text/javascript" src='<%= Request.ApplicationPath %> /js/jquery-1.2.2.pack.js'></script>

This will work

<script language="javascript" type="text/javascript" src='<%= Request.ApplicationPath %>/js/jquery-1.2.2.pack.js'></script>
 <iframe id="ads_frame"  marginheight="0" marginwidth="0"  scrolling="no" frameborder='0' src='<%= Request.ApplicationPath %>/slideAds.html’ > </iframe> 

Happy coding and all comments are welcome.

Comments (2) -

  • Jamorama

    5/6/2010 5:57:12 PM |

    Now this is highly recommeded post for me. I will surely email this to my friend.

  • VG

    7/2/2010 7:57:46 PM |

    Thank you, Good artical

Add comment