When and Why to Use Page.ResolveUrl in ASP.NET

asp.nethtmlwebforms

So, i had a discussion with a colleague on why and where should we use Page.ResolveUrl("~/PageUrl.aspx") instead of ./PageUrl.aspx.

For example, are there any differences between:

<a href='<%= Page.ResolveUrl("~/Home.aspx")%>' runat="server"> Home </a>

and

<a href="./Home.aspx" runat="server"> Home </a> ?

Here is what i found on msdn but the only benefit i see from there is that Page.ResolveUrl is good at resolving urls for content ( images & stuff like that)

Here you can find a good example on why to use Page.ResolveUrl but Babu Naresh Narra does not say anything about the ./ usage.

Are there any other benefits that Page.ResolveUrl gives over the basic ./ approach?

Best Answer

Suppose you had made a User Control that you were going to use on many different pages. Part of the User Control is a link to the Home page.

If you use the User Control on a page that is more than one level deep: i.e http://website/folder1/APageWithUserControlOn.aspx

then this :

<a href="./Home.aspx" runat="server"> Home </a>

will resolve to http://website/folder1/Home.aspx

Where as this:

 <a href="<%= Page.ResolveUrl("~/Home.aspx")%>" runat="server">

Will resolve to http://website/home.aspx which is much more likely to be what you wanted as the link will be correct on any page. The server side Page.Resolve resolved the url from the root when using '~'. You might thing you could use '/Home.aspx' in the first example, but this doesn't work as expected when using Virtual Directories.