Server.MapPath not working
I'm trying to access files in a virtual directory I created in IIS for the first time but am getting weird results.
O开发者_运维知识库S: 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?
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 to Server.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.
精彩评论