Azure Deployment - Unable to create a folder in ASP.NET Web API

MaruthiPrasad 21 Reputation points
2021-02-10T14:08:39.603+00:00

Hello Team,

Facing the same issue which HemantSudehely-0557 reported in this thread.

Using Web API , unable to create a folder (with two approaches with two folders - test1 and test2) within existing folder(ExLogger) which is in application root folder. Unable to attach files for reference. Below is the code as well as application folder structure.

Below is the error message for code tried below to it.

Could not find file 'C:\home\site\wwwroot\ExLogger\test1\'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost)
at System.IO.DirectoryInfo.Create()
at Demo.WebAPI.Controllers.AccountController.VerifyLogin(JObject juser) in D:\a\1\s\Demo.WebAPI\Controllers\AccountController.cs:line 1317

Code :

var appPath = AppDomain.CurrentDomain.RelativeSearchPath;
var tempappPath = Path.GetFullPath(Path.Combine(appPath, @"..\"));
DirectoryInfo dir = new DirectoryInfo(Path.Combine(tempappPath, "ExLogger\test1\"));
if (!dir.Exists)
{
dir.Create();
}
//app 1
dir = new DirectoryInfo(System.Web.Hosting.HostingEnvironment.MapPath("~/ExLogger/" + "test2"));
if (!dir.Exists)
{
dir.Create();
}

App Folder Structure :

MyWebAPI
-- Areas
--bin
--Content
-- ExLogger

Please help us on this.

Thanks & Regards,
MaruthiPrasad

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,408 questions
{count} votes

Accepted answer
  1. singhh-msft 2,431 Reputation points
    2021-02-18T11:27:27.467+00:00

    I suspect that you might be using ZipDeploy in Azure DevOps pipelines.

    Could you please check what is the deployment method in your Pipeline's Azure App Service Deploy task? It can be found as depicted in the image below:

    ![67379-image.png][2]

    If it is not Web Deploy, I request you to change it to Web Deploy. Further, don't forget to remove WEBSITE_RUN_FROM_ZIP App Setting from your App Service App.

    Now, after these changes, redeploy your API using your pipeline and let me know if it works.

    I would also suggest you to check out this article, which explains you about App Service's File handling.


    Please do not forget to "Accept the answer" if the information provided helps you. This will help others in the community as well.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Michael Taylor 51,346 Reputation points
    2021-02-10T15:13:39.6+00:00

    Web apps, even when running on a local IIS server, do not have write permissions to their own directory structure. The only directory they can write to is App_Data IIRC.

    You must either pre-create the directory at deployment (we just create the directory with a dummy file as part of our project structure) or you need to modify the permissions at deployment time to give your app write permissions. However I would strongly recommend you just create a base directory in your app under which you can store everything and then give your app permissions to write to that directory during deployment.