views:

81

answers:

4

Suppose I have a resource located in ~/Resources/R1.png This resource's relative URL will vary depending on the current address.

For instance:
If I'm at www.foo.com/A/B/C/D.aspx and the www.foo.com/A is the root path including Virtual Directory, then the path relative to the current address of ~/Resources/R1.png is ../../../Resources/R1.png

How can I get this relative path?

EDIT:

I want a web path that I can use in a web page, not a server path.

A: 
Server.Mappath("~/Resources/" + filename)
Muhammad Akhtar
This one gives you the file system path - e.g. d:\blah\blah...
Andrew Corkery
I was in hurry to leave the office and just give the idea, how can you accomplish this, but you are right.
Muhammad Akhtar
+1  A: 
string path = Request.ApplicationPath + "/Resources/R1.png";
dcp
This is equivalent to ResolveUrl, also solves my problem.
Ciwee
A: 

Get the Virtual Path:

Request.Path
Martin
+2  A: 

ResolveUrl("~/Resources/R1.png")

Where '~' is used to represent the root of the application in which the current page/control sits.

Or if the resource is external to the current application but is still found within the virtual directory hierarchy, you can use ResolveUrl("/Resources/R1.png")

pb
well, it doesnt convert to the ../../xx form. Instead, to converts to a /xx form that browsers can interpret.
Ciwee
You can always use ResolveClientUrl("~/Resources/R1.png") to get he ../../xxx form if you need that instead.
pb