当前位置:网站首页>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()
边栏推荐
- 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?
- Research and investment strategy report of China's electronic hydrogen peroxide industry (2022 Edition)
- After unplugging the network cable, does the original TCP connection still exist?
- Lauchpad x | MODE
- How do microservices aggregate API documents? This wave of show~
- pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
- 《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
- C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
- How to batch change file extensions in win10
猜你喜欢

Markdown syntax

Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310

Leetcode (Sword finger offer) - 35 Replication of complex linked list
![[untitled] forwarding least square method](/img/8b/4039715e42cb4dc3e1006e8094184a.png)
[untitled] forwarding least square method

PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql

How does idea withdraw code from remote push

Jianzhi offer 09 realizes queue with two stacks
![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)

How should PMP learning ideas be realized?

2022-2028 global intelligent interactive tablet industry research and trend analysis report
随机推荐
If you can quickly generate a dictionary from two lists
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
Sword finger offer 30 contains the stack of Min function
China electronic grade sulfur trioxide Market Forecast and investment strategy report (2022 Edition)
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
What is inner connection and outer connection? What are the uses and benefits
Problems encountered by scan, scanf and scanln in golang
Leetcode (Sword finger offer) - 35 Replication of complex linked list
如何编写单元测试用例
AMLOGIC gsensor debugging
LinkedList in the list set is stored in order
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
How do microservices aggregate API documents? This wave of show~
C语言-入门-基础-语法-[运算符,类型转换](六)
Fatal error in golang: concurrent map writes
Explanation of for loop in golang
Research Report on the development trend and Prospect of global and Chinese zinc antimonide market Ⓚ 2022 ~ 2027
Daughter love: frequency spectrum analysis of a piece of music
Mac platform forgets the root password of MySQL
Latex download installation record