简体   繁体   English

ASP.NET中的虚拟路径

[英]Virtual Path in ASP.NET

I am using this code for my LinkButton to download a file when you click on it. 当您单击它时,我正在使用我的LinkBut​​ton的代码下载文件。 Recently i got this problem, i need a virtual path . 最近我遇到了这个问题,我需要一条虚路。

I would like to redirect to C:\\inetpub\\wwwroot 我想重定向到C:\\inetpub\\wwwroot

 string filepath = Server.MapPath("...");           
 FileInfo myfile = new FileInfo(filepath);

So I don't know what to put in Server.MapPath() because if I put / it will go to the base of my folder documents and no In 所以我不知道要在Server.MapPath()中放入什么,因为如果我放入/它将进入我的文件夹文档的基础,而不会进入

You currently see a folder under your My Documents because you are running/debugging in your project folder, probably using the Development Server or IISExpress. 您当前在“ My Documents下看到一个文件夹,因为您可能正在使用Development Server或IISExpress在项目文件夹中运行/调试。

As soon as you deploy your site to a folder under C:\\Inetpub , the MapPath will return that folder. 将站点部署到C:\\Inetpub下的文件夹后, MapPath将返回该文件夹。

But, as a security measure, you cannot return a folder higher (in the tree) as the root folder from your application. 但是,作为安全措施,您不能将更高的文件夹(在树中)作为应用程序的根文件夹返回。

If you need that, you have to remove folders from the returned path yourself. 如果需要,您必须自己从返回的路径中删除文件夹。 You can use System.IO.Path for that. 您可以为此使用System.IO.Path

You are not allowed to move up from your root. 不允许您从根本上升级。 The root means the root folder of the application. 根目录是指应用程序的根文件夹。 If you are trying so, it is a security breach. 如果您尝试这样做,则是安全漏洞。

So if you wish to go to the root of the application you can use Server.MapPath("~"); 因此,如果您希望转到应用程序的根目录,则可以使用Server.MapPath("~");

If you need to serve files from another part of your system then the best way to do this is to create a virtual directory . 如果需要从系统的另一部分提供文件,则最好的方法是创建虚拟目录

In IIS right hand click on your website, click 'Add Virtual directory'. 在IIS的右键单击您的网站,单击“添加虚拟目录”。 Give it a name and point it whereever you need it to be. 给它起个名字,然后将其指向任何需要的地方。 Eg new virtual directory called 'files' pointing at 'C:\\inetpub\\wwwroot\\files'. 例如,名为“文件”的新虚拟目录指向“ C:\\ inetpub \\ wwwroot \\ files”。

Then from within your site you can reference these files by using 然后,您可以在您的网站中通过使用以下文件来引用这些文件

/files/filename.txt <- the /files/ will link to your virtual directory. /files/filename.txt <- / files /将链接到您的虚拟目录。

You will then of course need to make sure you have your permissions set correctly to read these files but I will leave that up to you. 然后,您当然需要确保已正确设置权限才能读取这些文件,但是我将由您自己决定。

Server.MapPath("~"); 

将为您提供当前的Web应用程序根目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM