Control.ResolveUrl versus Control.ResolveClientUrl versus VirtualPathUtility.ToAbsolute

Is there any benifit to using one of these methods over the other when resolving paths which start with the tilde (~)?

Generally, what is a better practice, should you be sending relative paths or absolute paths down in your html?

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

The difference between ResolveUrl and ResolveClientUrl is that ResolveClientUrl returns a path relative to the current page, ResolveUrl returns a path relative to the site root:

http://www.andornot.com/blog/post/ResolveUrl-vs-ResolveClientUrl.aspx

I would recommend using absolute paths.

Edit: Rick Strahl posted a nice article about this

Edit2: Removed bit about caching. Does not add to the answer and may not necessarily be accurate.

http://west-wind.com/weblog/posts/132081.aspx

Method 2

Here’s another article that explains the difference between the various ways to resolving paths in ASP.NET –

Different approaches for resolving URLs in ASP.NET

Method 3

Note that VirtualPathUtility.ToAbsolute(virtualPath) will throw an exception if a query string is included in the path.

The HttpException message will be along the lines of “‘~/YourVirtualPath/YourPage.aspx?YourQueryStringArg=FooBar’ is not a valid virtual path.”

See Rick Strahl’s Web Log:ResolveUrl() without Page
and MSDN: VirtualPathUtility.ToAbsolute Method (String)

Method 4

Another difference I noticed:

Code:

string value = "~/Docs/Hello & World.aspx";
Response.Write(HyperLink1.ResolveClientUrl(value) + "<br/>");
Response.Write(HyperLink1.ResolveUrl(value) + "<br/>");

Result:

Docs/Hello%20&%20World.aspx

/Docs/Hello & World.aspx


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