当前位置:网站首页>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()
边栏推荐
- LinkedList in the list set is stored in order
- How does idea withdraw code from remote push
- How to ensure the uniqueness of ID in distributed environment
- Trees and graphs (traversal)
- Write a jison parser from scratch (5/10): a brief introduction to the working principle of jison parser syntax
- How to display √ 2 on the command line terminal ̅? This is actually a blog's Unicode test article
- MySQL foundation 02 - installing MySQL in non docker version
- C语言-入门-基础-语法-[运算符,类型转换](六)
- 2022-2028 global small batch batch batch furnace industry research and trend analysis report
- Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
猜你喜欢
2022-2028 global strain gauge pressure sensor industry research and trend analysis report
Jianzhi offer 09 realizes queue with two stacks
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
2022-2028 research and trend analysis report on the global edible essence industry
MySQL foundation 02 - installing MySQL in non docker version
C语言-入门-基础-语法-数据类型(四)
If you can quickly generate a dictionary from two lists
2022-2028 global edible probiotic raw material industry research and trend analysis report
Markdown syntax
Ultimate bug finding method - two points
随机推荐
MySQL foundation 02 - installing MySQL in non docker version
什么是权限?什么是角色?什么是用户?
Some points needing attention in PMP learning
Solve the problem of "Chinese garbled MySQL fields"
GoLand environment variable configuration
《网络是怎么样连接的》读书笔记 - FTTH
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
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
20220701 Barbalat引理证明
Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
Write a jison parser from scratch (3/10): a good beginning is half the success -- "politics" (Aristotle)
How do microservices aggregate API documents? This wave of show~
2022-2028 global visual quality analyzer industry research and trend analysis report
Launpad | 基础知识
Write a jison parser from scratch (6/10): parse, not define syntax
Explanation of closures in golang
In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
Trees and graphs (traversal)