7

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?

1 Answer 1

10

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.

5
  • As far as i know in aspx, you can use something like <%@ Register Src="UserControl.ascx" TagName="UserControl" TagPrefix="uc1" %> to register an user control which is a best practice in my opinion. Your answer indeed gives another benefit for Page.ResolveUrl but i don't know if it's the most important one.
    – Zippy
    Mar 17, 2015 at 14:53
  • Not sure why you mention how to register a User Control on a page? My point was that the link was part of the User Control. The reason I used this example, is because in my experience this is the biggest reason for using Page.ResolveURL as opposed to straight relative html links and I didn't see you mention that as one of the benefits already.
    – Si-N
    Mar 17, 2015 at 15:24
  • I mentioned the registering of an User control because after it, you just have to type <uc:Name runat="server"/> and you've removed the problem with the Virtual Directories, right?. Please correct me if i'm wrong.
    – Zippy
    Mar 17, 2015 at 16:49
  • No sorry, you have missed my point. I meant suppose you have a user control for your navigation of your site called UCNavigation.ascx. All this control does is render out a load of menu links, 'Home', 'My Account', 'Help' etc. If you use Page.ResolveUrl to create this links for you to all these pages inside the user control, you know no matter on what pages you use an instance of the navigation user control the links will always link to the correct page. This is not the case if you use relative urls as in your other example. Hope I'm making sense.
    – Si-N
    Mar 17, 2015 at 17:26
  • Oh, yes, indeed i missed your point. I taught you were referring at calling an User control in other pages. It does make sense now.
    – Zippy
    Mar 17, 2015 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.