Asp. NET programming get Web site root Directory Method Summary _ Practical skills

Source: Internet
Author: User
Tags httpcontext

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&gt
  ;</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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.