Saturday, 28 March 2015

Approaches for resolving URLs in ASP.NET

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 – 
1)   Request.ApplicationPath
2)   System.Web.VirtualPathUtility
3)   Page.ResolveUrl
4)   Page.ResolveClientUrl
To show the difference, let's take an example. Let's say our application has 2 folders, with the following files – 
form1.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
b) When application is not in a sub-directory

1) Request.ApplicationPath
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.
In case b). Request.ApplicationPath will return "/". Notice there is a now a slash at the end, and sUrl gets a value.
In this case, rather than requesting "", the browser requests "http://Forms2/form2.aspx".

So, don't use Request.ApplicationPath to get relative paths to files. 

2) Page.ResolveClientUrl
sUrl = Page.ResolveClientUrl("~/Forms2/form2.aspx");
This will result in -
Notice that the path returned is relative to the calling page. 

3) Page.ResolveUrl
sUrl = Page.ResolveUrl("~/Forms2/form2.aspx");
This will result in -
Notice that the path returned in this case is relative to the site root, ie, from the top of the virtual directory.
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. 

4)   System.Web.VirtualPathUtility
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.

No comments:

Post a Comment