Home>

There are several ways to get the root directory of the website:

server.mappath (request.servervariables ["path_info"])

server.mappath ("/")

server.mappath ("") //The directory where the current code file is located

server.mappath (".")

server.mappath ("../")

server.mappath ("..")

page.request.applicationpath

The above code is at http://localhost/englishclub/manage/webform1.aspx page

operation 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 method can be used in.aspx access, but if you are in.CS files cannot be used.

httpcontext.current.server.mappath ();

system.web.httpcontext.current.request.physicalapplicationpath

Available in .cs files.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 to be obtained.This should be used when writing the database path,Others have problems.

system.web.httpcontext.current.request.physicalapplicationpath

It has the same effect as server.mappath ("~/").

server.mappath ("~/");//Regardless of the file's path strength,Always return c:\ inetpub \ wwwroot \ englishclub \ (the root directory where the current program is running)

If Road King, which stores attachments, enters the database,Absolute road strength should not be saved.Only the filename part should be stored.E.g:

/uploads/abc.txt

When you need to browse files,In the path read out:(ie /uploads/abc.txt), in front of the website + Road King:For example:

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

supplement:

Asp.net get the complete example of the website root directory and physical path:

///<summary>
///Get the URL of the root directory of the website
///</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 on the web site
      apppath=urlauthority;
    else
      //Install in a virtual subdirectory
      apppath=urlauthority + req.applicationpath;
  }
  return apppath;
}
///<summary>
///Get the URL of the root directory of the website
///</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 on the web site
      apppath=urlauthority;
    else
      //Install in a virtual subdirectory
      apppath=urlauthority + req.applicationpath;
  }
  return apppath;
}
///<summary>
///Get the physical path of the website root directory
///</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;
}
  • Previous Understand the development of iOS multi-threaded applications and how to create threads
  • Next C # method based on delegation to achieve operations between multiple threads