RSS

Tag Archives: ResolveUrl in Javascript

Use of ResolveUrl


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

MapPath(ResolveUrl(“~/App_Data/MyResource”))

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

Page.ResolveUrl("~/layout/images/andornotLogo.gif") 
"/layout/images/andornotLogo.gif"
Page.ResolveClientUrl("~/layout/images/andornotLogo.gif")
"../layout/images/andornotLogo.gif"

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=http://www.asp.com").
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();

sbUrl.Append(HttpRuntime.AppDomainAppVirtualPath);

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

sbUrl.Append(‘/’);

// 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;

}

else

foundSlash = false;

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

{

if (c == ‘?’)

foundQM = true;

else

{

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

{

if (foundSlash) continue;

else

{

sbUrl.Append(‘/’);

foundSlash = true; continue;

} }

else if

(foundSlash) foundSlash = false;

} }  sbUrl.Append(c);

} return

sbUrl.ToString();

}

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

 
6 Comments

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

 

Tags: , ,