Resolve URL paths

When using MasterPages, Cascading Style Sheets (CSS) and JavaScript files it is quite hard to keep track of all URL paths. Rather than changing the path for each page when referencing the CSS file or any other file which is located in different paths, the ResolveURL can be used. Below are quick examples on how each can be used:

CSS:

<link href=”<%= ResolveUrl(“~/”) %>cssFolder/myStyle.css” rel=”stylesheet” type=”text/css” />

style on html / aspx page:

<div style="background:url('<%= ResolveUrl("~/myImages/myFavImage.jpg") %>') repeat;"> </div>

JavaScript:

<script type=”text/javascript” src=”<%= ResolveUrl(“~/”) %>js/jquery.min.js”></script>

Dynamic URL using jQuery FancyBox:

<a href=”#” onclick=”$.fancybox({‘href’ : ‘<%= ResolveUrl(“~/”) %>myFolder/myPage.html’,’type’:’iframe’,’width’: 900,’height’: 500});return false;”>My Page</a>

Error: Cannot use a leading .. to exit above the top directory.

Error: Cannot use a leading .. to exit above the top directory.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Cannot use a leading .. to exit above the top directory.

Solution:

Check The Master page or any Default Page currently using before error appears.

Make sure that references in the <head> </head> section do not go above directory listings.

simple basic example:

If your page is in the main project:

ERROR:         <link href=”../CSS/help.css” rel=”stylesheet” type=”text/css” />

SOLTION:    <link href=”CSS/help.css” rel=”stylesheet” type=”text/css” /> //Removing the ../ 

                        <link href=”<%= ResolveUrl(“~/”) %>CSS/help.css” rel=”stylesheet” type=”text/css” /> //When using ASP.NET we can resolve the URL when not sure about the path