What is the difference between ResolveUrl and ResolveClientUrl?

C#asp.net

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

Here's a simple example:

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

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

//Returns: C:\inetpub\wwwroot\MyProject\HomePage.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;

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShantanu GuptaView Question on Stackoverflow
Solution 1 - C#BrandonView Answer on Stackoverflow
Solution 2 - C#LDawggieView Answer on Stackoverflow
Solution 3 - C#JuriView Answer on Stackoverflow
Solution 4 - C#ZolfaghariView Answer on Stackoverflow
Solution 5 - C#Hasan AbbasView Answer on Stackoverflow