12

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

Chandu
  • 81,493
  • 19
  • 133
  • 134
user576510
  • 5,777
  • 20
  • 81
  • 144
  • some suggestions here might help:http://stackoverflow.com/questions/6544096/static-content-on-development-and-production-envoirnment – Daniel Powell Jul 06 '11 at 12:22

6 Answers6

14

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
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • @user576510 - try second option – Pranay Rana Jul 06 '11 at 12:36
  • 2
    Works great. I was calling a page inside an iFrame for testing on my development machine and it uses LocalHost then some random port number that I didn't want to hard-code. Applying your suggestion like this: Request.Url.GetLeftPart(UriPartial.Authority) + Page.ResolveUrl("TestHttpMethods.aspx") did the trick and gave me this: "http://localhost:62053/Pages/Forms/TestHttpMethods.aspx". Thanks! – MikeTeeVee Sep 24 '12 at 23:25
1

Request.Url.AbsoluteUri is the way to go

Bergkamp
  • 599
  • 2
  • 6
  • 22
1

Use this. Its working

System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/" + System.Web.HttpContext.Current.Request.ApplicationPath + "/" + folderpath + filename;

0

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.

detale
  • 12,482
  • 4
  • 41
  • 42
0

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.

negaboys
  • 42
  • 4
-1

try this:

Server.MapPath("~/");
       or 
Request.Url.Host
Ovais Khatri
  • 3,201
  • 16
  • 14