Click here to Skip to main content
15,881,588 members
Articles / All Topics

What's the Difference in Page Class Function ResolveURL vs ResolveClientURL

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
16 Sep 2011CPOL1 min read 19.4K   6   3
Difference in Page class function ResolveURL vs ResolveClientURL

The .NET Page class contains two methods, ResolveUrl and ResolveClientUrl which at first glance look the same. They both take the same relative URL and return browser friendly URl. So why 2 functions to do the same job? Though their functionality is the same, but their way of returning client browser relative URL makes the huge difference. ResolveClientUrl returns a path relative to the current page which user is currently browsing, and ResolveUrl returns a path relative to the site root that is configured as root in IIS.

Let's consider this structure:

ROOT
–App_Themes
–Images
—-mypic.png
–MyAccount
—-default.aspx 

Now from above example, root of the IIS directory is ROOT, and all other directories are under this root. Now from MyAccount->default.aspx tries to access Images->mypic.png. ResolveUrl will do top to bottom parsing to find the resource, on the other hand ResolveClientUrl will do bottom up parsing to find the same resource.

Function: Page.ResolveUrl("~/Images/mypic.png")

Returns: "/Images/mypic.png"

Function: Page.ResolveClientUrl("~/Images/mypic.png")

Returns: "../Images/mypic.png"

The difference between these two is very visible if you host your web app under a shared hosting under a subdomain. For example, you host your MY_SITE app under www.example.com subdomain, and your physical file path is example.com\www\MY_SITE under IIS directory. So under this, you placed your file structure given above. Now for any link, if you set:

C#
Page.ResolveUrl("~/MyAccount/default.aspx"), 

Watch out for the URL in browser status bar, you will see www.example.comwww/MY_SITE/MyAccount/default.aspx, because ResolveUrl will parse from top root domain for this resource, so it took full path from root for that specific resource. On the other hand, ResolveClientUrl can solve this problem. This will parse from browsers' present location up towards the root, so it finds the specific resource path.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Bangladesh Bangladesh
I am a Software Engineer and Microsoft .NET technology enthusiast. Professionally I worked on several business domains and on diverse platforms. I love to learn and share new .net technology and my experience I gather in my engineering career. You can find me from here

Personal Site
Personal Blog
FB MS enthusiasts group
About Me

Comments and Discussions

 
GeneralMy vote of 5 Pin
Prabhu Singh15-Feb-13 1:27
Prabhu Singh15-Feb-13 1:27 
Concise info. Just what I needed to know.
GeneralRe: My vote of 5 Pin
Shahriar Iqbal Chowdhury/Galib15-Feb-13 20:54
professionalShahriar Iqbal Chowdhury/Galib15-Feb-13 20:54 
GeneralMy vote of 5 Pin
Sk. Tajbir9-Jul-12 4:03
Sk. Tajbir9-Jul-12 4:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.