Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure static files System.IO.DirectoryNotFoundException

I have an ASP.NET Core application running on Azure. The application sends various emails - both in plaintext and HTML formats. I have created a subfolder in our Resources folder, called EmailTemplates, where I store all the HTML and TXT templates. An example path could be:

\src\WebProject\Resources\EmailTemplates\Welcome.html

I access the file in my EmailService.cs class in the following manner:

var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "EmailTemplates", "Welcome.html");
var htmlTemplate = await File.ReadAllTextAsync(filePath);

This works great when running on local environment both on IIS and Kestrel. However, when deployed to Azure, I get an IO error message:

System.IO.DirectoryNotFoundException
An unhandled exception has occurred: Could not find a part of the path 'D:\home\site\wwwroot\Resources\EmailTemplates\Welcome.html'.

I have found a similar question on StackOverflow, but the answer is almost 6 years old. I am not sure, whether it still applies since Azure has changed significantly in the past couple of years. I was also looking at official documentation (1) (2), but I did not find anything special about Azure.

What is the correct way to store and retrieve email templates (and other static files) in ASP.NET Core and Azure?

like image 311
lss Avatar asked Jun 22 '18 06:06

lss


1 Answers

Make sure your static files are part of your project.

And mark them explicitly as content and Copy to output folder -> always.

like image 199
astaykov Avatar answered Nov 17 '22 14:11

astaykov