Server.MapPath does not find the path on Azure

dsb picture dsb · Nov 18, 2015 · Viewed 8.9k times · Source

I have deployed my project to Azure. In my project I have "App_Data\Images" folder.

Now I'm trying to do the following:

String filename = GLOBAL_IMAGES_VALS.GET_FILE_PREFIX(imageType) + "-" + User.Identity.GetUserId<int>().ToString() + Path.GetExtension(image.FileName);

String origPath = Server.MapPath("~\\App_Data")+"\\Images\\"  + filename;

But then upon trying:

image.SaveAs(origPath);

I get this error message:

Could not find a part of the path 'D:\home\site\wwwroot\App_Data\Images\logo-10003065.jpg'.

How can I save my file to "App_Data\Images\"?

Answer

dsb picture dsb · May 19, 2016

The actual problem was that the sub-folder 'Images' did not exist. I can't remember why the publish process did not create this sub-folder, however I added it manually and then everything worked fine.

EDIT:

As others wrote here (@Spectarion). I'll put here the important remark that explain why the folder was not created:

Just for the future readers, folder won't be created if it's empty. Folder is empty even if there are files that are not included in project.

Just put some 'fake.txt' file into any folder you want to make sure that it will be created, and of course don't forget to add it to the project. Good luck.