Virtual Path in ASP.NET

做~自己de王妃 提交于 2019-12-13 05:18:27

问题


I am using this code for my LinkButton to download a file when you click on it. Recently i got this problem, i need a virtual path .

I would like to redirect to 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


回答1:


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'. Give it a name and point it whereever you need it to be. E.g. new virtual directory called 'files' pointing at '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.

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.




回答2:


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.

As soon as you deploy your site to a folder under C:\Inetpub, the MapPath will return that folder.

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.




回答3:


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("~");




回答4:


Server.MapPath("~"); 

will give you current web application root.



来源:https://stackoverflow.com/questions/8309001/virtual-path-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!