Microsoft Technologies » .Net Framework
What are the different approaches for resolving URLs in ASP.Net, and how do they differ?
By "resolving URLs in ASP.Net", what we are referring to is how does a control resolve relative/absolute path while looking for a resource on the server-side and making it available on the client side.
For example, why is there a difference in the rendering with the following two approaches:
<img src='~/images/edujini-labs-logo.jpg' runat='server' />
and
<img src='<%= ResolveUrl("~/images/edujini-labs-logo.jpg" %>' />The class System.Web.UI.Control provides two methods - ResolveClientUrl and ResolveUrl which have different approaches for resolving the URLs. The former approach uses ResolveClientUrl method while in the latter we are using ResolveUrl method.
The method ResolveUrl in conjunction with two little-explored but very powerful properties - TemplateSourceDirectory and AppRelativeTemplateSourceDirectory - defined in the class System.Web.UI.Control.
The first property gets the "virtual directory of the Page or UserControl object that contains the current server-control"!
The property AppRelativeTemplateSourceDirectory returns the "application-relative virtual directory of the Page or UserControl object that contains the current server-control".
So, what we end up in getting is a path relative to application-relative virtual directory, ie, from the top of the virtual directory. For example, the path that we may get is "/VirtualDirectory-Name/images/edujini-labs-logo.jpg".
The method ResolveClientUrl returns a path that is relative to the current-page. So, if the page is in the folder "/VirtualDirectory-Name", the value returned will be "images/edujini-labs-logo.jpg", where as if the page is in the folder "/VirtualDirectory-Name/F1/F2", the value returned will be "../../images/edujini-labs-logo.jpg".
Last update: 2007-06-23 20:40
Author: Edujini Labs Pvt Ltd
Revision: 1.3
Print this record
Send to a friend
Show this as PDF file
Export as XML-File
You cannot comment on this entry