繁体   English   中英

从 c# 中的 Azure Function 返回 HTML

[英]Return HTML from an Azure Function in c#

我在 c# 中写了一个 Azure function,它返回 html。当我从 web 浏览器发出请求时,它会将完整的响应显示为原始文本,而不是将其呈现为ContentType我想我需要 8 on the608 response5.8 我试过这个答案,但似乎我需要一个 nuget package... 并且变得复杂。

如何在 Azure Function 的响应中设置 ContentType header?

下面是一种仅使用System.Net命名空间(不需要添加任何引用或 nuget 包)在来自 Azure Function 的响应上设置ContentType的方法。 在这种情况下,要让浏览器呈现 html,请设置"text/html"

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ILogger log)
{
    var html = "<html><head></head><body>Example Content</body></html>";
    var response = req.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(html, Encoding.UTF8, "text/html");

    return response;
}

第一个答案是 .NET Framework,如果你需要 .NET Core/6.0...

var html = "<html><head></head><body>Example Content</body></html>";

return new ContentResult()
{
    Content = html,
    ContentType = "text/html",
    StatusCode = 200
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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