This article describes the ASP.net programming to get the Web site root directory method. Share to everyone for your reference, specific as follows:
There are several ways to get the site root directory:
Server.MapPath (request.servervariables["Path_info"])
Server.MapPath ("/")
Server.MapPath ("")//the directory where the current code file is located road strength
Server.MapPath (".")
Server.MapPath (".. /")
Server.MapPath ("..")
Page.Request.ApplicationPath
The above code is in the Http://localhost/EnglishClub/manage/WebForm1.aspx page
Run Result:
C:\Inetpub\wwwroot\EnglishClub\manage\WebForm1.aspx
C:\Inetpub\wwwroot\
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\
C:\Inetpub\wwwroot\EnglishClub
The above methods can be accessed in. aspx, but if you're in. CS file can not be used.
HttpContext.Current.Server.MapPath ();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath
Available in the. cs file. But HttpContext.Current.Server.MapPath (); This gets the path to the file rather than the root directory.
Only System.Web.HttpContext.Current.Request.PhysicalApplicationPath this is to get the root directory, in the write get database path is supposed to use this, all the others have problems.
System.Web.HttpContext.Current.Request.PhysicalApplicationPath
and Server.MapPath ("~/") effect is the same.
Server.MapPath ("~/"), regardless of the file in which the code is located, the page path strength is what, always return to C:\Inetpub\wwwroot\EnglishClub\ (is the current program running the root directory)
If the path to the storage attachment to the database, you should not put the absolute road strength into. Only the file name portion should be stored. For example:
/uploads/abc.txt
When you need to browse the file, read the path: (ie/uploads/abc.txt), the front + site's road strength: for example:
http://abc.com+ "/uploads/abc.txt"
Add:
Asp. NET to get the complete instance of the site root and physical path:
<summary>///Gets the URL///</summary>///<returns></returns> public static string Getroo of the site's root directory
Turi () {String apppath = "";
HttpContext httpcurrent = HttpContext.Current;
HttpRequest Req;
if (httpcurrent!= null) {Req = Httpcurrent.request;
String urlauthority = Req.Url.GetLeftPart (uripartial.authority); if (Req.applicationpath = null | |
Req.applicationpath = = "/")//directly installed in the WEB site AppPath = urlauthority;
else//installed under the virtual subdirectory AppPath = urlauthority + Req.applicationpath;
return AppPath; ///<summary>///Gets the URL of the root directory of the Web site///</summary>///<param name= "Req" ></param>///<returns>
;</returns> public static string Getrooturi (HttpRequest Req) {string apppath = "";
if (Req!= null) {string urlauthority = Req.Url.GetLeftPart (uripartial.authority); if (Req.applicationpath = null | | Req.applicationpath = = "/")//directly installed in the WEB site AppPath = UrlauthoriTy
else//installed under the virtual subdirectory AppPath = urlauthority + Req.applicationpath;
return AppPath; ///<summary>///Gets the physical path to the Web site root///</summary>///<returns></returns> public static string Getro
Otpath () {String apppath = "";
HttpContext httpcurrent = HttpContext.Current;
if (httpcurrent!= null) {AppPath = HttpCurrent.Server.MapPath ("~");
else {AppPath = AppDomain.CurrentDomain.BaseDirectory; if (Regex.match (AppPath, @ "\\$", regexoptions.compiled).
Success) AppPath = apppath.substring (0, apppath.length-1);
return AppPath;
}
I hope this article will help you with ASP.net programming.