简体   繁体   中英

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?

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:

  1. Return HTML or file content from C# Azure function.
  2. C# Azure functions separate html and css.
  3. Can we display html page which we deployed on to Azure function app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM