Use of ResolveUrl

06 May

ResolveUrl  use for directory folder path

ResolveUrl use for CSS File Include in Master page or any where

Recommend Procedure  This Method

<link href=”<%=Page.ResolveUrl(“~/”)%>includes/menu.css” rel=”stylesheet” type=”text/css” />

ResolveUrl use for js or images File Include in Master page or any where

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

<img border=”0″ src=”<%=Page.ResolveUrl(“~/”)%>images/ir-top.jpg” alt=”” />

Another Way to use ResolveUrl


Here is what we would get if we called the two methods from our Default.aspx page.


Third Way

 resolve relative url’s without ResolveUrl

Sometimes you need to resolve relative url’s without ResolveUrl. If the code is executing outside a Control, for example in an IHttpHandler or business layer code somewhere that has no reference to a Control, you can’t call Control.ResolveUrl.

The System.Web.VirtualPathUtility class has some very useful method for converting from an app relative path to an absolute path:

    string absoluteUrl = VirtualPathUtility.ToAbsolute(relativeUrl);

ResolveUrl in Javascript

In the master page for the site, put this:

<script type="text/javascript"> var baseUrl = "<%= ResolveUrl("~/") %>"; </script>

Then, in your javascript file, put this function:

function ResolveUrl(url) { if (url.indexOf("~/") == 0)  { url = baseUrl + url.substring(2); } return url;  }

Fourth Way

Some Time you use Difficulty  URL  and not correctly handle It. For example, try Page.ResolveUrl("~/sajjad.aspx?param2=").
By looking into the ASP.NET code using Reflector, I found that all mechanisms that are supposed to convert the relative URLs to absolute URLs will search first for a “://” inside the string, and will return you if found. So a query string is OK, unless you pass in a parameter with ://.
Yes, I know that the query string parameter should be UrlEncode

suggested methods on the web involve using VirtualPathUtility.ToAbsolute, which is pretty nice and handy for you

Url Code

The Perfect variable that will give me the Application Virtual Path at runtime without a page context.

HttpRuntime.AppDomainAppVirtualPath. It will work anywhere – even inside a timer callback! It gives the path without a trailing slash

Then, I did some tests on the original ResolveUrl code, and found where I need to replace what with the AppVirtualPath:

  1. When the URL begins with a slash (either / or \), it will not touch it!
  2. When the URL begins with ~/, it will replace it with the AppVirtualPath.
  3. When the URL is an absolute URL, it will not touch it. (ResolveUrl has a bug with this, as I said before…)
  4. In any other case (even beginning with ~, but not slash), it will append the URL to the AppVirtualPath.
  5. Whenever it modifies the URL, it also fixes up the slashes. Removes double slashes and replaces \ with /.

So I replicated all of that, but without the bugs. And here’s the code:

public static string ResolveUrl(string relativeUrl)

{  if (relativeUrl == null)

throw new ArgumentNullException(“relativeUrl”);

if (relativeUrl.Length == 0 || relativeUrl[0] == ‘/’ || relativeUrl[0] == ‘\\’) return

relativeUrl; int idxOfScheme = relativeUrl.IndexOf(@”://”, StringComparison.Ordinal);

if (idxOfScheme != -1)


int idxOfQM = relativeUrl.IndexOf(‘?’);

if (idxOfQM == -1 || idxOfQM > idxOfScheme) return relativeUrl;


StringBuilder sbUrl = new StringBuilder();


if (sbUrl.Length == 0 || sbUrl[sbUrl.Length – 1] != ‘/’)


// found question mark already? query string, do not touch!

bool foundQM = false;

bool foundSlash; // the latest char was a slash?

if (relativeUrl.Length > 1 && relativeUrl[0] == ‘~’ && (relativeUrl[1] == ‘/’ || relativeUrl[1] == ‘\\’))

{ relativeUrl = relativeUrl.Substring(2);

foundSlash = true;



foundSlash = false;

foreach (char c in relativeUrl)  { if (!foundQM)


if (c == ‘?’)

foundQM = true;



if (c == ‘/’ || c == ‘\\’)


if (foundSlash) continue;




foundSlash = true; continue;

} }

else if

(foundSlash) foundSlash = false;

} }  sbUrl.Append(c);

} return



<link href=”<%=Page.ResolveUrl(“~/”)%>includes/menu.css” rel=”stylesheet” type=”text/css” />


Posted by on May 6, 2011 in ASP Dot Net C#, Java Script, Vb.Net


Tags: , ,

6 responses to “Use of ResolveUrl

  1. garage door

    May 27, 2011 at 8:25 pm

    where can I sub to ur rss?

    • humrahimcs

      June 4, 2011 at 9:16 pm

      Thank Dear for visit and give good remarks.
      which is give me more confidence to add more article which i face and resolve the problem and upload for other people ..

      Yaa you can get rss of blog.
      just you visit and see the top of the page…..

  2. vinod

    August 26, 2011 at 11:48 am

    nice description dear… i like it

  3. app spying

    November 22, 2013 at 3:49 am

    Hello, i think that i noticed you visited my blog so i got here to return
    the favor?.I am trying to in finding things to enhance my website!I guess its adequate to make use of some of your concepts!!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: