There are various ways that are available in ASP.NET that we can use to resolve relative paths to a resource on the server-side and making it available on the client-side. I know of 4 ways -
To show the difference, let's take an example. Let's say our application has 2 folders, with the following files -
/Forms1/forml.aspxform1.aspx redirects to form2.aspx and needs to get the path to that file.
We will consider 2 case -
a.) When application is hosted in a virtual directory
http://www.server.com/virtual-directory/Forms2/form2.aspxb.) When application is not in a sub-directory
The most common way people use this method is by concatenating strings to create the Url to a resource.
sUrl = Request.ApplicationPath + "/Forms2/form2.aspx";In case a), Request.ApplicationPath will return "/virtual-directory". Notice there is no slash at the end, and the above sUrl gets a value
/vitural-directory/Forms2/form2.aspxIn case b). Request.ApplicationPath will return "/". Notice there is a now a slash at the end, and sUrl gets a value
//Forms2/form2.aspxIn this case, rather than requesting "http://www.server.com/Forms2/form2.aspx", the browser requests "http://Forms2/form2.aspx".
So, don't use Request.ApplicationPath to get relative paths to files.
sUrl = Page.ResolveClientUrl("~/Forms2/form2.aspx");Notice that the path returned is relative to the calling page.
This will result in -
sUrl = Page.ResolveUrl("~/Forms2/form2.aspx");Notice that the path returned in this case is relative to the site root, ie, from the top of the virtual directory.
This will result in -
So which one is better between ResolveClientUrl and ResolveUrl? I don't think one is better than the other, but most people use Page.ResolveUrl.
sUrl = VirtualPathUtility.ToAbsolute("~/Forms2/form2.aspx");
This will resolve, same as Page.ResolveUrl, to
Whats the diffirence between VirtualPathUtility and ResolveUrl? Well, there are a few -
- ResolveUrl can only be called in the context of a Page. But VirtualPathUtility can be used from anywhere.
- VirtualPathUtility will throw an error if there are Query String parameters in the URL -
// throws error
sUrl = VirtualPathUtility.ToAbsolute("~/Forms2/form2.aspx?q=123");
// works as expected
sUrl = Page.ResolveUrl("~/Forms2/form2.aspx?q=123");
To conclude, I think Page.ResolveUrl should be used anytime you are in the context of the Page. Everywhere else, use VirtualPathUtility.