Similarities and differences of Server.MapPath and Request.physicalapplicationpath

Source: Internet
Author: User

A lot of people are not unfamiliar with them, in many Web applications, using Server.MapPath and Request.physicalapplicationpath to manipulate the directory/file is a mixed chance, I used to mix, but a long time. Found Request.physicalapplicationpath a little "trouble"?
In general applications, there is no sense of the difference between the two, and in some of the existence of "virtual directory", the existence of multi-level subdirectories, the need for virtual directory mutual file operation of the site, you can know their differences. My summary is as follows:
1, file operations relative to the current path:
You can use Server.MapPath ("Test.txt"), Server.MapPath ("A/test.txt"), Server.MapPath (".. /b/test.txt "), this time using Request.physicalapplicationpath is difficult to locate the file;
2, the file operation of the absolute path:
At this time both are OK, It is important to note that Server.MapPath is absolutely "oriented" to the site, not because the "virtual directory" itself is different from the properties of the Request.physicalapplicationpath is not the case.
For example: The primary site points to D:\wwwroot\, the virtual directory points to E:\wwwroot\a\, named a.
Then if I want to operate the E:\wwwroot\a\test.txt under the virtual directory, I can use Server.MapPath ("/a/test.txt") or use Server.MapPath directly ("~/test.txt");
So how do I write with Request.physicalapplicationpath?
Many people are: Request.physicalapplicationpath + "test.txt", I said not necessarily, why?
Because if the virtual directory has a separate application pool, Request.physicalapplicationpath is E:\wwwroot\a\, followed by Test.txt is E:\wwwroot\a\test.txt;
But if the virtual directory does not have a separate application pool, Request.physicalapplicationpath is D:\wwwroot\, no matter how you add it, you can't locate the file I want to find. Oh ...

3. File operations across virtual directories:
For example I am now operating virtual directory B under the Test2.txt file, using Server.MapPath ("/b/test2.txt") there is no problem (if I have permission to operate, no permissions is another matter).
And Request.physicalapplicationpath can not find other virtual directories under the file, do not believe who try, then tell me how?!!

Similarities and differences of Server.MapPath and Request.physicalapplicationpath

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.