当前位置:网站首页>ASP. Net to access directory files outside the project website
ASP. Net to access directory files outside the project website
2022-07-04 09:33:00 【Dandelion_ drq】
To put it simply , It can be done by IIS The way to add a virtual directory is , When getting access paths, use HttpContext.Current.Server.MapPath("~/xxx"); The way .
Now let's talk about how to do it in detail ……
First of all to see IIS The project website structure deployed above :

There are two project websites ,NewsAPI and FileAPI, Now I want to FileAPI Next visit NewsAPI Under the html Catalog .
So I am FileAPI A new one named html The virtual directory of , Point to NewsAPI Under the html Folder .

Be careful :
Good configuration IIS after , Also set VS In local debugging mode , To run the code .

Next is the code :
/// <summary>
/// Test whether you can access files in other project directories
/// </summary>
/// <returns></returns>
public string Get()
{
string str = "";
try
{
string path = System.Web.HttpContext.Current.Server.MapPath("~/html/1.txt");
StreamReader sr = new StreamReader(path, System.Text.Encoding.GetEncoding("UTF-8"));
str = sr.ReadToEnd(); // Read the file
sr.Dispose();
}
catch (System.Exception ex)
{
str = " Wrong report !" + ex.ToString();
}
return str;
} It's just that the test I didn't write is very complicated , Is the use of HttpContext.Current.Server.MapPath("~/xxx"); Get the actual physical path according to the virtual directory , Then read the files in the directory and return ,path The resulting value is the physical path , So you can visit .
You can make a breakpoint to see path Value

OK!~
Last , About Server.MapPath You can see the usage of this article :server.mappath()
边栏推荐
- How does idea withdraw code from remote push
- Fatal error in golang: concurrent map writes
- How to write unit test cases
- 2022-2028 global special starch industry research and trend analysis report
- How do microservices aggregate API documents? This wave of show~
- Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'
- What is permission? What is a role? What are users?
- Are there any principal guaranteed financial products in 2022?
- 保姆级JDEC增删改查练习
- Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
猜你喜欢

2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors

C language - Introduction - Foundation - syntax - data type (4)

pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.

Explain TCP protocol in detail three handshakes and four waves

HMS core helps baby bus show high-quality children's digital content to global developers

How to ensure the uniqueness of ID in distributed environment

PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f

LeetCode 74. Search 2D matrix

MySQL foundation 02 - installing MySQL in non docker version

Daughter love: frequency spectrum analysis of a piece of music
随机推荐
GoLand environment variable configuration
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
[on February 11, 2022, the latest and most fully available script library collection of the whole network, a total of 23]
《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
Ultimate bug finding method - two points
After unplugging the network cable, does the original TCP connection still exist?
Global and Chinese markets of water heaters in Saudi Arabia 2022-2028: Research Report on technology, participants, trends, market size and share
《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
Research Report on the current market situation and development prospects of calcium sulfate whiskers in China (2022 Edition)
lolcat
How should PMP learning ideas be realized?
Write a jison parser from scratch (1/10):jison, not JSON
Tkinter Huarong Road 4x4 tutorial II
2022-2028 research and trend analysis report on the global edible essence industry
Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
Explain TCP protocol in detail three handshakes and four waves
ArrayBuffer
2022-2028 global strain gauge pressure sensor industry research and trend analysis report