'How to run HTML at runtime in C# using azure function?
I have a file XHTMLTemplate.cs which is as follows:
namespace sqlicmalertfunctionsapp
{
public class XHTMLTemplates
{
public static string GenerateXHTMLDescriptionFromAppLogs(List<TableData> transfers)
{
string description = "<html>"
+ "<head> <style> "
+ "table, th, td { border: 1px solid black; border-collapse: collapse;}"
+ "th td { font-weight: bold; }"
+ "th, td { padding: 5px; text-align: left; }"
+ "</style> </head>"
+ "<body>"
+ "<div> "
+ "<h2 style=\"color: #fff; margin: 0; background-color: #6495ed\"> Table Name </h2>"
+ "<br/>"
+ "<table>"
+ "<tr>"
+ "<th> CustomerNbr </th>"
+ "<th> CustomerName </th>"
+ "<th> MSOrderNumber </th>"
+ "<th> Quantity </th>"
+ "<th> PromoQuantity </th>"
+ "<th> QtyDiff </th>"
+ "<th> NetAmount </th>"
+ "<th> PromoNetAmount </th>"
+ "<th> AmtDiff </th>"
+ "<th> ExtendedAmount </th>"
+ "<th> PromoExtendedAmount </th>"
+ "<th> isICMSent </th>"
+ "</tr>"
+ string.Join(" ",
transfers.Select(t => (
"<tr>"
+ $"<td> {t.CustomerNbr} </td>"
+ $"<td> {t.CustomerName} </td>"
+ $"<td> {t.MSOrderNumber} </td>"
+ $"<td> {t.Quantity} </td>"
+ $"<td> {t.PromoQuantity} </td>"
+ $"<td> {t.QtyDiff} </td>"
+ $"<td> {t.NetAmount} </td>"
+ $"<td> {t.PromoNetAmount} </td>"
+ $"<td> {t.AmtDiff} </td>"
+ $"<td> {t.ExtendedAmount} </td>"
+ $"<td> {t.PromoExtendedAmount} </td>"
+ $"<td> {t.isICMSent} </td>"
+ "</tr>"
)))
+ "</table>"
+ "</div>"
+ "</body>"
+ "</html>";
return description;
}
}
}
I have a time triggered azure function which pushes table rows to this template at runtime and gives a string of html file. How to display this string using azure function? Are there any other alternatives of displaying HTML code using azure function?
Solution 1:[1]
Below is the example script for adding HTML script as an attachment and running timer triggered function.
using System.Net;
using System.Net.Http.Headers;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
string html = @"<html>
<head><title>Hello world!</title></head>
<body>
<h1>Hello World!</h1>
<p>from my <strong>Azure Function</strong></p>
</body>
</html>";
var result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(System.Text.Encoding.UTF8.GetBytes(html));
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{ FileName = "file.html" };
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
}
Here are the related links:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SaiSakethGuduru-MT |
