問題描述
如何在本地主機和服務器上獲取 URL 路徑? (how to get URL path on local host and on server?)
Plase guide me how to get URL path excluding page name on localhost and server.
for example for the page Active.aspx local path that I want to get is here in bold.
*http://localhost:1532/WebFolder/*Active.aspx
and on server I want to get this bold part
*http://domain.com/WebFolder/*Active.aspx
Similarly if page is in root it will return
*http://domain.com/Active.aspx or *http://localhost:1532/**Active.aspx
‑‑‑‑‑
參考解法
方法 1:
Request.ApplicationPath
‑ Gets the ASP.NET application's virtual application root path on the server.
Request.Path
‑ Gets the virtual path of the current request.
Edit
To get domain + current request + virtual path of the current application, try below:
Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
方法 2:
Request.Url.AbsoluteUri is the way to go
方法 3:
You can use Request.servervariable["Remote_addr"] to get the ip address.But if you try on local host, it will returns default ip address of your machine. You can check this code from web server, can get ip address.
方法 4:
If you just want to get the local file path of a page, eg. "Active.aspx" Use
Request.AppRelativeCurrentExecutionFilePath
which ignores your localhost, local host file mapping, or virtual directory name and will return "~/Active.aspx" Other properties such as Request.Url.LocalPath won't help.
方法 5:
Use this. Its working
System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/" + System.Web.HttpContext.Current.Request.ApplicationPath + "/" + folderpath + filename;
(by user576510、Pranay Rana、Bergkamp、negaboys、detale、jayesh goswami)