Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I am getting Physical path given, virtual path expected error while I used for getting a pdf file in my controller..
C#
string filePath = Server.MapPath(Url.Content("D:\\test.pdf"));
in my controller..

Please help..
Posted
Updated 21-Jul-13 23:20pm
v2

Hello

Use like

C#
string filePath = Server.MapPath("~\test.pdf"); 

It's necessary the file physical location is in your application folder.
 
Share this answer
 
v2
Comments
Apali 22-Jul-13 5:13am    
but still I am getting this D:/test.pdf' is a physical path, but a virtual path was expected. after using your code...
Actually I want my D drive folder in controller
Manohar_manu 22-Jul-13 5:23am    
refer this link
http://www.dotnetperls.com/mappath

Manohar_manu 22-Jul-13 5:24am    
http://www.west-wind.com/weblog/posts/2009/Dec/21/Making-Sense-of-ASPNET-Paths
Harshad Kathiriya 22-Jul-13 5:26am    
May i know, What's actual scenario ?
So I'll get proper solution...
Apali 22-Jul-13 5:31am    
In my D drive test.pdf file is there..In my MVC application I want to display that pdf file in my view page..so in controller I am using like this ViewData["Message"] = filePath; and in my view page I have used <% var message = ViewData["Message"] as string; %> <%=message%>....

so I used like below code..
string filePath = Server.MapPath(Url.Content("D:\\test.pdf"));
ViewData["Message"] = filePath;
If you already know your folder is: D:\\test.pdf then you do not need to use Server.MapPath.
Or,
Alternatively it can be said,if you already have a physical path, it doesn't make sense to call Server.MapPath.
Also see..
a physical path, but a virtual path was expected in C#[^]
http://social.msdn.microsoft.com/Forums/vstudio/en-US/78d08afd-77b0-4b2e-a19f-30686f906960/capplicationfilestesttxt-is-a-physical-path-but-a-virtual-path-was-expected[^]
 
Share this answer
 
Comments
Apali 22-Jul-13 5:39am    
Hi ridoy,

In my D drive test.pdf file is there..In my MVC application I want to display that pdf file in my view page..so in controller I am using like this ViewData["Message"] = filePath;

and in my view page I have used <% var message = ViewData["Message"] as string; %> <%=message%>.... so I used like below code.. string filePath = Server.MapPath(Url.Content("D:\\test.pdf")); ViewData["Message"] = filePath;

can you pls help to find that pdf file in my view page..
ridoy 22-Jul-13 6:47am    
See http://stackoverflow.com/questions/11524293/displaying-pdf-asp-net-mvc,http://stackoverflow.com/questions/15121876/how-do-i-display-a-pdf-using-pdfsharp-in-asp-net-mvc, http://stackoverflow.com/questions/1881248/mvc-open-pdf-file
ridoy 22-Jul-13 7:07am    
try relativeDir = "~/data" + System.IO.Path.DirectorySeparatorChar + "test.pdf";
absoluteDir = Server.MapPath( relativeDir ); ViewData["Message"] = absoluteDir; where data is a folder within my solution explorer of test project.use your directory address there.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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