我尝试从 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
以下是浏览器中响应的样子:
知道如何设置内容类型以便浏览器解释 HTML 吗?
最佳答案
您可以返回一个带有属性 body
、headers
、status
和 isRaw
(可选)的 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/