Andre Pena
Andre Pena

Reputation: 59336

What is the best way to get a path relative to the current address?

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.

Upvotes: 3

Views: 198

Answers (3)

pb.
pb.

Reputation: 461

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")

Upvotes: 2

Martin
Martin

Reputation: 11041

Get the Virtual Path:

Request.Path

Upvotes: 0

dcp
dcp

Reputation: 55444

string path = Request.ApplicationPath + "/Resources/R1.png";

Upvotes: 1

Related Questions