Get root path, root url and any url of your website asp.net

This technique will help you to get url and path of server that you want.
Let’s see this result.

When I place this script on this path below :

http://www.aspgod.com/test/mappath/Default.aspx

And the script is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Request.Url.AbsoluteUri  //  http://www.aspgod.com/test/mappath/Default.aspx
Request.Url.AbsolutePath  //  /test/mappath/Default.aspx
Request.ApplicationPath  //  because I'm not set the virsual directory indeep of the server
 
Server.MapPath("") // d:\xxx\zzz\aspgod.com\httpdocs\test\mapath
Server.MapPath("Default.aspx") // d:\xxx\zzz\aspgod.com\httpdocs\test\mapath\Default.aspx
 
if you want to get the root path and root url.
You must enter this script below:
 
Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath,string.Empty) + Request.ApplicationPath
//  www.aspgod.com/
Server.MapPath(this.Request.ApplicationPath).Replace("/", "\\")
-> d:\xxx\zzz\aspgod.com\httpdocs

—–

If I use this script on localhost the the result is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Request.Url.AbsoluteUri  //  http://localhost:1904/aspgod/test/mappath/Default.aspx
Request.Url.AbsolutePath  //  /test/mappath/Default.aspx
Request.ApplicationPath  //  /aspgod
 
my project is in this path E:\--- Other Project ---\aspgod\program\aspgod
Server.MapPath("") // E:\--- Other Project ---\aspgod\program\aspgod\test\mapath
Server.MapPath("Default.aspx")
// E:\--- Other Project ---\aspgod\program\aspgod\test\mapath\Default.aspx
 
if you want to get the root path and root url.
You must enter this script below:
 
Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath,string.Empty) + Request.ApplicationPath
//  http://localhost:1904/aspgod
Server.MapPath(this.Request.ApplicationPath).Replace("/", "\\")
// E:\--- Other Project ---\aspgod\program\aspgod