Search code examples
c#exceptionpathserver.mappath

Server.MapPath gives wrong path, exception "The given path's format is not supported" when running on IIS server?


Currently, I'm using the codes below to save a file to a directory on my website.

//save the file to the server
String savePath = Server.MapPath("..\\temp\\") + file;
FileUpload.PostedFile.SaveAs(savePath);

When I'm running the application on my computer, it returns the path:

"E:\dotnet\Project\Implementation\Source Code\Project\Project.UI\temp\Sample.csv"

However, when running on a real server, it throws exception:

System.NotSupportedException: The given path's format is not supported.

The expected path that I would like to have is "C:\inetpub\wwwroot\Project\temp".

Am I doing things correctly or not? If not then can someone explain to me the correct way to do this?


Solution

  • Server.MapPath maps a virtual path to a physical one on the server. If you replace your backslashes with forward-slashes you should be ok:

    Server.MapPath("../temp/")
    

    You can also use the root relative path:

    Server.MapPath("~/temp/");