Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created a Virtual Directory (MyServerFiles) in my IIS 7.5. The PDF files loaded in the virtual folder are viewable through a browser. In my ASP.Net project, I coded like that below:
C#
string s = "http://myIPaddress/MyServerFiles/fw.pdf";
string template = HttpContext.Current.Server.MapPath(s);

But I got HttpException: "http://myIPaddress/MyServerFiles/fw.pdf" is not a valid virtual path. What's wrong in my code? Thanks for your help.

What I have tried:

How to Server.MapPath a file ...
Posted
Updated 16-Jun-16 5:25am

The easy answer is for you to google how to use Server.MapPath. First result is to MSDN, Server.MapPath Method[^]

Server.MapPath takes a virtual path. So, it would be something like

C#
string template = Server.MapPath("~/MyServerFiles/fw.pdf");


Then template will end up having something like "c:\inetpub\wwwroot\mysite\MyServerFiles\fw.pdf"
 
Share this answer
 
Comments
s yu 16-Jun-16 11:33am    
Thanks. If the Virtual Directory is not on my local IIS, how to do it?
ZurdoDev 16-Jun-16 11:36am    
What do you mean? Where is it?

If it is somewhere else then don't use Server.MapPath but get the path somehow else.
You can only map the path to files in that IIS application, so the files in your project effectively. Use "~" to denote the root;

MapPath("~/MyServerFiles/fw.pdf")

If your app is in a virtual directory then "~" represents the virtual directory folder, don't add the folder name yourself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900