What is the difference between ResolveUrl and ResolveClientUrl?

I have been using ResolveUrl for adding CSS and Javascript in ASP.NET files.

But I usually see an option of ResolveClientUrl. What is the difference between both?

When should I use ResolveClientUrl?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

ResolveUrl creates the URL relative to the root.

ResolveClientUrl creates the URL relative to the current page.

You can also use whichever one you want, however ResolveUrl is more commonly used.

Method 2

Here’s a simple example:

//Returns: ../HomePage.aspx
String ClientURL = ResolveClientUrl("~/HomePage.aspx");

//Returns: /HomePage.aspx
String RegURL = ResolveUrl("~/HomePage.aspx");

//Returns: C:inetpubwwwrootMyProjectHomePage.aspx
String ServerMappedPath = Server.MapPath("~/HomePage.aspx");

//Returns: ~/HomePage.aspx
String appRelVirtPath = AppRelativeVirtualPath;

//Returns: http://localhost:4913/
String baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;

//Returns: "http://localhost:4913/HomePage.aspx"
String absUri = Request.Url.AbsoluteUri;

Method 3

According to the MSDN documentation:

ResolveClientUrl

A fully qualified URL to the specified
resource suitable for use on the
browser.

Use the ResolveClientUrl method to
return a URL string suitable for use
by the client to access resources on
the Web server, such as image files,
links to additional pages, and so on.

ResolveUrl

The converted URL.

If the relativeUrl parameter contains an absolute URL, the URL is returned unchanged. If the relativeUrl parameter contains a relative URL, that URL is changed to a relative URL that is correct for the current request path, so that the browser can resolve the URL.

For example, consider the following
scenario:

A client has requested an ASP.NET page
that contains a user control that has
an image associated with it.

The ASP.NET page is located at
/Store/page1.aspx.

The user control is located at
/Store/UserControls/UC1.ascx.

The image file is located at
/UserControls/Images/Image1.jpg.

If the user control passes the
relative path to the image (that is,
/Store/UserControls/Images/Image1.jpg)
to the ResolveUrl method, the method
will return the value
/Images/Image1.jpg.

I think this explains it quite well.

Method 4

In short:

Page.ResolveUrl(~): creates the URL from the root of app.

and

Page.ResolveClientUrl(~): creates the URL relative to the current page.(e.g: ../../..)

but in my tests in asp.net, Page.ResolveUrl is better because of stable output & is not relative.

Method 5

Using Page.ResolveUrl is better if you are trying to get a Javascript friendly Url.

Like if you are opening an iframe from the parent page, you would need a full url that would be passed to the iframe src property.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x