How to add subfolders to the azure functions app using VScode?

Adwait Naik 40 Reputation points
2024-05-01T10:03:16.4+00:00

Hello there,

User's image

I have sub folders "assets" and "components" inside the content directory of my azure function app. I want to copy these folders to the output directory. How to do that?

I tried including this to my .csproj file but it didn't work for me:

<ItemGroup>
  <None Update="content/**/*.*">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>


<ItemGroup>
  <None Update="content/**/*.*">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>

Please could you suggest something?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,368 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 13,946 Reputation points
    2024-05-03T16:16:31.8666667+00:00

    Hi @Adwait Naik Greetings! Thank you for posting the question here.I have tried the below setting in my .csproj file and could push the sub folders to Azure function.

        <None Update="Content\**\**">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        </None>
    
    

    Please note that the output directory is used during development, while the publish directory is used when the project is published.

    Once I deployed the code, I could see the folders under the Files section in VS Code. Please find the below image for reference.

    User's image

    The folder directories are added under the Azure function C:\home\site\wwwroot directory. In the above example, to access welcome.html file, you can access it through the path C:\home\site\wwwroot\Content\Files\welcome.html

    Here is a snippet of the code I have tested to see if the files can be accessed.

            public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
            {
                _logger.LogInformation("C# HTTP trigger function processed a request.");   
                string baseFolderPath = @"C:\home\site\wwwroot\Content";         
                string fileRelativePath = @"Files\welcome.html";            
                string fullPath = Path.Combine(baseFolderPath, fileRelativePath);                                    
                string html = File.ReadAllText(fullPath);
                return new ContentResult
                {
                    Content = html,
                    ContentType = "text/html",
                    StatusCode = 200
                };
    
            }
    

    Hope this helps! Please let us know if you have any additional questions or issues.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.