powershell - 从 Azure PowerShell 函数提供 HTML 页面

标签 powershell azure azure-functions serverless faas

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

这是一个example from 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;
}

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

# 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

以下是浏览器中响应的样子:

enter image description here

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

最佳答案

您可以返回一个带有属性 bodyheadersstatusisRaw(可选)的 Response 对象:

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

关于powershell - 从 Azure PowerShell 函数提供 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49301247/

相关文章:

azure - Visual Studio 2019 无法更新 Azure Web 应用中的 wwwroot 文件夹内容

azure - azure函数应用程序设置连接字符串值中密码中的分号

powershell - 为什么我的 foreach 变量在每次迭代后没有进入 PowerShell 中的输出?

node.js - Bot 框架中的上下文管理 - Node JS

powershell - 如何使用PowerShell从Excel提取数据并通过邮件自动发送

c# - Azure Durable Functions - OrchestrationTrigger 连续执行第一个等待的事件

azure - Powershell 脚本不会列出过期的 key 保管库证书

c# - 我可以在 Azure Functions 2.0 中同时使用 dotnet 和 node 吗?

powershell - 如何从 PowerShell 写入 .CMD 文件?

powershell - 基本Powershell-将Word Docx批量转换为PDF