If, from an ASP.NET page, you need to access or refer to a file that resides in a different directory, you have a number of options to refer to that file. You can use an absolute path, such as
C:\Inetpub\wwwroot\DirName\FileName
or
/DirName/FileName
, or you can use a relative path, such as
../../DirName/FileName
if it's not in a subdirectory of the ASP.NET page or
DirName/FileName
if it is.
The problem with either of these approaches is that they are sensitive to changes in the site's layout or configuration. With absolute paths you can run into difficulties if you use different base application directories for development and production (i.e.,
using a virtual directory as the application root in a development server, but hosting the live version of the site in the root directory of the website). With relative paths things can go haywire if the ASP.NET page that references the file is moved to another
directory.
A workaround for this is to use the
~
character in your file paths. This character references the application's root. Therefore, you can refer to the file using
~/DirName/FileName
, and things will work even if the application root changes or if the ASP.NET page referencing the file is moved. Many ASP.NET server controls allow for this type of syntax. For example, in the HyperLink and Image controls you can set the
NavigateUrl
and
ImageUrl
properties using the
~
. If you need to programmatically resolve a URL that starts with
~
you can use the
ResolveUrl(string)
method, which can be found in the
Control
class. Example syntax:
Returns the correct path based on the application's root. For example, if the application was rooted at the virtual directory Foo, the return value would be /Foo/Images/Skyline.jpg...
'VB.NET... Dim url as String = Page.ResolveUrl("~/Images/Skyline.jpg")
John Federer
Member
193 Points
312 Posts
what is Page.ResolveUrl
Nov 04, 2009 07:17 AM|LINK
Hi
Can any one tell me the purpose of using Page.ResolveUrl
-Thanks
XIII
All-Star
182690 Points
23458 Posts
ASPInsiders
Moderator
MVP
Re: what is Page.ResolveUrl
Nov 04, 2009 08:08 AM|LINK
Hi,
taken from MSDN (http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx):
Converts a URL into one that is usable on the requesting client.
Before you ask that this method is from the Control class take into account that the Page class inherits itself from Control in the end.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
irokhes
Star
9920 Points
1333 Posts
Re: what is Page.ResolveUrl
Nov 04, 2009 08:11 AM|LINK
Hi,
Page.ResolveUrl to always return its URLs as an app-absolute path.
Resolve URL always gives you the root of virtual directory.
e.g. if you say ResolveURL("~/default.aspx");
then it gives you path "Web/Default.aspx". where web is path of your virtual directory.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx
mY BlOg
Babunareshna...
Participant
968 Points
191 Posts
Re: what is Page.ResolveUrl
Nov 04, 2009 08:35 AM|LINK
Refer this
http://bytes.com/topic/asp-net/answers/577294-difference-between-server-mappath-page-resolveurl
If, from an ASP.NET page, you need to access or refer to a file that resides in a different directory, you have a number of options to refer to that file. You can use an absolute path, such as
or , or you can use a relative path, such as if it's not in a subdirectory of the ASP.NET page or if it is.The problem with either of these approaches is that they are sensitive to changes in the site's layout or configuration. With absolute paths you can run into difficulties if you use different base application directories for development and production (i.e., using a virtual directory as the application root in a development server, but hosting the live version of the site in the root directory of the website). With relative paths things can go haywire if the ASP.NET page that references the file is moved to another directory.
A workaround for this is to use the
character in your file paths. This character references the application's root. Therefore, you can refer to the file using , and things will work even if the application root changes or if the ASP.NET page referencing the file is moved. Many ASP.NET server controls allow for this type of syntax. For example, in the HyperLink and Image controls you can set the and properties using the . If you need to programmatically resolve a URL that starts with you can use the method, which can be found in the class. Example syntax:(Thanks to Phil Winstanley for this suggestion!) from http://4guysfromrolla.com/articles/052505-1.aspx
Hope this helps.
Babu Naresh Narra
Remember to click “Mark as Answer” on the post If you get answer from my post(s) !