Summary of ASP. NET Programming Method for Obtaining Website Root Directory

  • 2021-07-09 07:49:47
  • OfStack

This article describes the example of ASP. NET programming to obtain the website root directory method. Share it for your reference, as follows:

There are several ways to get the root directory of a website, such as:

Server.MapPath(Request.ServerVariables["PATH_INFO"])
Server.MapPath("/")
Server. MapPath ("")//Directory where the current code file is located
Server.MapPath(".")
Server.MapPath("../")
Server.MapPath("..")
Page.Request.ApplicationPath

The above code is on the http://localhost/EnglishClub/manage/WebForm1.aspx page

Run results:

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 are in. The cs file does not work.

HttpContext.Current.Server.MapPath();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath

Available in the. cs file. But HttpContext. Current. Server. MapPath (); This gets the path of the file instead of the root directory.

Only System. Web. HttpContext. Current. Request. PhysicalApplicationPath is the root directory of the fetch, which should be used when writing the fetch database path. Everything else has problems.

System.Web.HttpContext.Current.Request.PhysicalApplicationPath
And Server. MapPath ("~/") have the same effect.

Server. MapPath ("~/"); //Always return C:\ Inetpub\ wwwroot\ EnglishClub\ regardless of the page strength of the file where the code is located (which is the root directory where the current program is running)

If the way to store attachments is in the database, the absolute way should not be stored. Only the filename portion should be stored. For example:

/uploads/abc.txt
When you need to browse files, in the read out path: (i.e./uploads/abc. txt), the front + the road strength of the website: for example:

http://abc.com+"/uploads/abc.txt"

Additional:

ASP. NET to get a complete example of the Web site root and physical path:


/// <summary>
///  Object of the root directory of the website URL
/// </summary>
/// <returns></returns>
public static string GetRootURI()
{
  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 == "/")
      // Install directly in   Web   Site   
      AppPath = UrlAuthority;
    else
      // Installed in a virtual subdirectory   
      AppPath = UrlAuthority + Req.ApplicationPath;
  }
  return AppPath;
}
/// <summary>
///  Object of the root directory of the website URL
/// </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 == "/")
      // Install directly in   Web   Site   
      AppPath = UrlAuthority;
    else
      // Installed in a virtual subdirectory   
      AppPath = UrlAuthority + Req.ApplicationPath;
  }
  return AppPath;
}
/// <summary>
///  Gets the physical path of the Web site root 
/// </summary>
/// <returns></returns>
public static string GetRootPath()
{
  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 is helpful to everyone's asp. net programming.


Related articles: