当前位置:网站首页>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()
边栏推荐
- MySQL transaction mvcc principle
- Reading notes on how to connect the network - hubs, routers and routers (III)
- How to ensure the uniqueness of ID in distributed environment
- Launpad | Basics
- You can see the employment prospects of PMP project management
- Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
- After unplugging the network cable, does the original TCP connection still exist?
- 2022-2028 global strain gauge pressure sensor industry research and trend analysis report
- 《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
- Implementation principle of redis string and sorted set
猜你喜欢
![Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)

LeetCode 74. Search 2D matrix

PHP student achievement management system, the database uses mysql, including source code and database SQL files, with the login management function of students and teachers

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

2022-2028 global elastic strain sensor industry research and trend analysis report

Opencv environment construction (I)

SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache

Some points needing attention in PMP learning

Logstack configuration details -- elasticstack (elk) work notes 020

2022-2028 global intelligent interactive tablet industry research and trend analysis report
随机推荐
Talk about single case mode
Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
【leetcode】29. Divide two numbers
el-table单选并隐藏全选框
Lauchpad X | 模式
Daughter love: frequency spectrum analysis of a piece of music
Explanation of closures in golang
The child container margin top acts on the parent container
Global and Chinese trisodium bicarbonate operation mode and future development forecast report Ⓢ 2022 ~ 2027
Awk from entry to earth (14) awk output redirection
Mac platform forgets the root password of MySQL
Get the source code in the mask with the help of shims
How should PMP learning ideas be realized?
How to display √ 2 on the command line terminal ̅? This is actually a blog's Unicode test article
2022-2028 research and trend analysis report on the global edible essence industry
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
Ultimate bug finding method - two points
《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
C語言-入門-基礎-語法-[運算符,類型轉換](六)