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

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

For example:
If I am in www.foo.com/A/B/C/D.aspx, and www.foo.com/Ais the root path, including Virtual Directory, then the path to the current address ~/Resources/R1.pngis../../../Resources/R1.png

How can I get this relative path?

EDIT:

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

+3
source share
3 answers

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

'~' , / .

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

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

Get the virtual path:

Request.Path
0
source

Source: https://habr.com/ru/post/1729560/


All Articles