Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ResolveUrl and ResolveClientUrl?

Tags:

c#

asp.net

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?

like image 366
Shantanu Gupta Avatar asked Dec 09 '09 15:12

Shantanu Gupta


3 Answers

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.

like image 195
Brandon Avatar answered Nov 07 '22 23:11

Brandon


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;
like image 40
LDawggie Avatar answered Nov 07 '22 23:11

LDawggie


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.

like image 13
Juri Avatar answered Nov 08 '22 00:11

Juri