0

i want to get the fully qualified url to a resource in ASP.NET.

e.g.:

<LINK rel="shortcut icon" href="<%=GetFaviconPath()%>">

with the code-behind file right now containing:

private String GetFaviconPath()
{
   String url = System.Web.VirtualPathUtility.ToAbsolute("~/Images/clock.ico");
   return url;
}

Unfortunately this doesn't work because it doesn't return the fully qualified path, only the path relative to the server:

/Employement/Images/clock.ico

Internet Explorer requires a fully qualified url, e.g.:

http://localhost:62119/Employment/Images/clock.ico

http://avenger:81/Employment/Images/clock.ico

http://MyFreeAspDotNetHosting.com/IanBoyd/Employment/Images/clock.ico

How can i get the fully qualified path to a file? i've tried VirtualPathUtility and i'm all out of ideas.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

2 Answers2

2

You could append what you have to the result of

Request.Url.GetLeftPart(UriPartial.Authority)

Also, have a look at System.UriBuilder http://msdn.microsoft.com/en-us/library/wdwhd34a.aspx

CRice
  • 12,279
  • 7
  • 57
  • 84
1

Try this

string _ApplicationPath = HttpContext.Current.Request.Url.ToString();

Append your relative path to that absolute path.

Martin Doms
  • 8,598
  • 11
  • 43
  • 60