Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting html file from path for an Azure Function

I have a html file that is an email template. In my Azure function I want to read the file in with 'HtmlDocument', manipulate it, then send it out as an email to members.

How do I read the file 'hero.html' from my Azure Function app? And then once I publish it to Azure will I need to change the way the file is read?

FYI - doc.Load accepts a string, file path and other parameters

Here is what I tried and a pic of my project.

        //var emailBodyText = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Templates", "hero.html"));
        var mappedPath = Path.GetFullPath("hero.html");

        var doc = new HtmlDocument();
        doc.Load(mappedPath);

enter image description here

like image 843
user1186050 Avatar asked Oct 03 '17 03:10

user1186050


People also ask

Can you trigger an Azure function using an HTTP request?

The HTTP trigger lets you invoke a function with an HTTP request. You can use an HTTP trigger to build serverless APIs and respond to webhooks. The default return value for an HTTP-triggered function is: HTTP 204 No Content with an empty body in Functions 2.

What is ExecutionContext in Azure function?

ExecutionContext. The execution context enables interaction with the Azure Functions execution environment. HttpRequestMessage<T> An HttpRequestMessage instance is provided to Azure functions that use HttpTrigger.


1 Answers

You can get the function directory name from ExecutionContext parameter, if you add it to the function:

public static HttpResponseMessage Run(HttpRequestMessage req, ExecutionContext context)
{
    var path = $"{context.FunctionDirectory}\\Templates\\hero.html";
    // ...
}

See details in Retrieving information about the currently running function.

like image 73
Mikhail Shilkov Avatar answered Oct 13 '22 06:10

Mikhail Shilkov