35

I have a virtual path (example: "~/Images/Banner.jpg") and I want to make that an absolute web path (example: "/ApplicationRoot/Images/Banner.jpg"). There is a method that will do this, I believe in a class called something like HTTPUtility or similar name. Though ever time I need this method, it takes me hours searching for it. It would be greatly appreciated if someone could post the proper method to do this so I can favorite this for easy access in the future.

Thank you very much.

1
  • @JoelBeckham You are right. I misunderstood the question. Commented Oct 27, 2011 at 23:08

3 Answers 3

66
System.Web.VirtualPathUtility.ToAbsolute("yourRelativePath");

There you go :)

Sign up to request clarification or add additional context in comments.

4 Comments

I actually had the same problem untill I made my own PathHelper class for the asp.net mvc framework :p
Hehe, yet another place to do this! I guess they all end up at the same place.
This was exactly the method I keep forgetting.. I don't know why it's so hard for me to remember or find this one.
Thank you again.. It has been a year, I still can't remember that command, and I still go to here every time to remember it.
13

There are various ways that are available in ASP.NET that we can use to resolve relative paths to absolute Urls -

1) Request.ApplicationPath
2) System.Web.VirtualPathUtility
3) Page.ResolveUrl
4) Page.ResolveClientUrl

Here's a article that explains the difference between the various ways to resolving paths in ASP.NET -

Different approaches for resolving URLs in ASP.NET

1 Comment

Um. I don't think there is a Page.ResolveClientUrl. The method exists in Control apparently: msdn.microsoft.com/en-us/library/…
5
Control.ResolveClientUrl(url)

or

Control.ResolveUrl(url)

Whichever one you need (honestly I dont know the difference, as both seem to return mostly the same, perhaps someone can illuminate me :) ).

2 Comments

How do you make your verry first line a "code" line? In preview it looks all biscuits and milk, but when I submit it becomes an unformatted line again :s
I didnt have that problem. Using FF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.