Server.MapPath 不工作
我第一次尝试访问我在 IIS 中创建的虚拟目录中的文件,但得到了奇怪的结果。
操作系统:Windows XP Pro
IIS:5.1
开发平台:Visual Studio 2008
语言:C#
虚拟目录名称:portal
本地路径:C:\digital_communications_report_portal
默认网站本地路径:c:\inetpub\wwwroot
我可以在 http://localhost/portal/testFile.xlsx 没有任何问题。
我尝试其中任何一个
string realFilename = Server.MapPath(ResolveUrl("~/portal/testFile.xlsx"));
string realFilename = Server.MapPath(ResolveUrl("localhost/portal/testFile.xlsx"));
并得到“C:\Documents and Settings\jjohnson\My Documents\Visual Studio 2008\WebSites\clientsite\localhost\portal\testFile.xlsx”,这是我的项目路径,虚拟路径打在末尾,不是有效路径。我尝试使用波形符或 localhost,但收到“无法映射路径 '/portal/testFile.xlsx'”。例外。
有什么想法我做错了吗?
I'm trying to access files in a virtual directory I created in IIS for the first time but am getting weird results.
OS: Windows XP Pro
IIS: 5.1
Development Platform: Visual Studio 2008
Language: C#
Virtual Directory Name: portal
Local Path: C:\digital_communications_report_portal
Default Website Local Path: c:\inetpub\wwwroot
I can download the file at http://localhost/portal/testFile.xlsx without any problem.
I try either of these
string realFilename = Server.MapPath(ResolveUrl("~/portal/testFile.xlsx"));
string realFilename = Server.MapPath(ResolveUrl("localhost/portal/testFile.xlsx"));
and get "C:\Documents and Settings\jjohnson\My Documents\Visual Studio 2008\WebSites\clientsite\localhost\portal\testFile.xlsx" which is my project path with the virtual path slapped on the end and is not a valid path. I try taking the tilde or localhost and I get a "Failed to map the path '/portal/testFile.xlsx'." exception.
Any ideas what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话,
Server.MapPath(ResolveUrl("~/relative/path/to/file"))
不是您想要的。它将生成一个/rooted/path/to/file
,当传递给Server.MapPath()
时,它不会产生有效的位置。相反,只需使用
Server.MapPath("~/relative/path/to/file");
更新
您遇到的问题是由于您包含虚拟目录名称(“portal”) ) 在您的相对 URL 中。您的相对 URL 不需要指定虚拟目录名称,只需指定所需文件相对于应用程序根目录的路径即可。
If I'm not mistaken,
Server.MapPath(ResolveUrl("~/relative/path/to/file"))
is not what you want. It will produce a/rooted/path/to/file
which when passed toServer.MapPath()
will not yield a valid location.Instead simply use
Server.MapPath("~/relative/path/to/file");
Update
The problem you are experiencing is due to the fact that you are including the virtual directory name ("portal") in your relative URL. Your relative URL does not need to specify the virtual directory name, just the path to the desired file relative to the root of the application.