从 Azure PowerShell 函数提供 HTML 页面

Mar*_*ndl 2 powershell azure azure-functions faas serverless

我尝试从 Azure PowerShell函数提供 HTML 页面。我能够返回 HTML,但我知道在哪里可以将内容类型设置为 text/htm l 以便浏览器解释 HTML。

以下是Anythony Chu 提供的示例,说明如何使用 C# 实现此目的:

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
{
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    var stream = new FileStream(@"d:\home\site\wwwroot\ShoppingList\index.html", FileMode.Open);
    response.Content = new StreamContent(stream);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
    return response;
}
Run Code Online (Sandbox Code Playgroud)

但在 PowerShell 函数中,我只是使用 cmdlet 返回文件Out-File,并且没有设置内容类型的选项。这是一个你好世界的例子:

# POST method: $req
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$name = $requestBody.name

# GET method: each querystring parameter is its own variable
if ($req_query_name) 
{
    $name = $req_query_name 
}

$html = @'
<html>
<header><title>This is title</title></header>
<body>
Hello world
</body>
</html>
'@

Out-File -Encoding Ascii -FilePath $res -inputObject $html
Run Code Online (Sandbox Code Playgroud)

浏览器中的响应如下所示:

在此输入图像描述

知道如何设置内容类型以便浏览器解释 HTML?

Mik*_*kov 5

body您可以返回具有属性、headers和(可选)status的Response 对象:isRaw

$result = [string]::Format('{{ "status": 200, "body": "{0}", "headers": {{ 
"content-type": "text/html" }} }}', $html)
Out-File -Encoding Ascii $res -inputObject $result;
Run Code Online (Sandbox Code Playgroud)