. NET ways to get the Web site root directory

Source: Internet
Author: User
Tags httpcontext

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


Server.MapPath (request.servervariables["Path_info"]) Server.MapPath ("/") Server.MapPath ("")//the directory in which the current code file is located road strengthServer.MapPath (".") Server.MapPath (".. /") Server.MapPath ("..")

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

The above methods can be accessed in. aspx, but if you are in. CS file is not available.

HttpContext.Current.Server.MapPath ();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath can be used in. cs files. 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 the root directory to get, in the write to get the database path is should use this, the others have problems.


and Server.MapPath ("~/") effect is the same.

Server.MapPath ("~/");//Regardless of the file in which the code resides, what the page path is, always return to C:\Inetpub\wwwroot\EnglishClub\ (the root directory where the current program is running)

The absolute path should not be saved if the path to the storage attachment is entered into the database. Only the file name section should be stored. For example:


When you need to browse the file, in the path read out: (that is,/uploads/abc.txt), front + Web site of the road: for example:

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

Transferred from: http://www.cnblogs.com/wenbo/archive/2011/08/09/2132620.html


. NET ways to get the Web site root directory

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.